Search code examples
androidandroid-actionbarandroid-menu

How to change the hint text of search box in action bar using xml menu in Android?


I am developing an Android app. In my app, I am using action bar using AppCompactActivity. I implemented search box in the action bar successfully. But I have a small problem with that. I want to change the hint text in the search box.

See the screenshot below:

enter image description here

This is my menu.xml with search item

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/office_action_search"
        android:orderInCategory="97"
        android:title="Search"
        android:icon="@android:drawable/ic_menu_search"
        app:showAsAction="always"
        app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

This is how I set menu with search feature for action bar:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.office_main_menu, menu);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

        SearchView search = (SearchView) menu.findItem(R.id.office_action_search).getActionView();
        search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
        search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //tabLayout.getTabAt(0).select();
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    return true;
}

How can I change hint text of search box?


Solution

  • You can customize the hint of the SearchView with the method setQueryHint.

    Modify your code like this:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.office_main_menu, menu);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    
            SearchView search = (SearchView) menu.findItem(R.id.office_action_search).getActionView();
            search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
            search.setQueryHint("Your custom hint");
            search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String query) {
                    //tabLayout.getTabAt(0).select();
                    return true;
                }
    
                @Override
                public boolean onQueryTextChange(String newText) {
                    return false;
                }
            });
        }
    
        return true;
    }