V
V
Vasily Vorobyov2015-09-01 08:27:55
Android
Vasily Vorobyov, 2015-09-01 08:27:55

How to make custom SearchView in Toolbar in Android?

This is how I create the SearchView in the Toolbar.

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_toolbar, menu);

        MenuItem searchItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) searchItem.getActionView();

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        if(null!=searchManager ) {
            searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        }
        searchView.setIconifiedByDefault(false);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

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

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

    <item
        android:id="@+id/action_search"
        android:title="@android:string/search_go"
        android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
        yourapp:showAsAction="always|collapseActionView"
        yourapp:actionViewClass="android.support.v7.widget.SearchView" />

</menu>

Thus, when you click on the magnifying glass, the input pops out, and when something is written inside, a cross pops out, with which you can clear everything. I would like to add search next and search previous buttons next to the cross, for navigational search. How to implement it?
UPD :
For clarity:
73ef07d19ca945fd97e89feccd3f640d.png

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question