Android SearchView on ActionBar, detect click on Search

rodi picture rodi · Jun 20, 2014 · Viewed 15.6k times · Source

In my application, like many other, there is a common search widget (http://developer.android.com/guide/topics/search/search-dialog.html#UsingSearchWidget).

I would change fragment (and pass to it the term searched) only when the search button on the keyboard is pressed.

I've tried with

searchView.setOnSearchClickListener(new OnClickListener() { 
@Override public void onClick(View v) { 
} });

but it is triggered when you press the button ON THE ACTION BAR, that uncollapse the search input, and not when the keybord button search is clicked.

Do you know how is possible?

Answer

Timur Gilfanov picture Timur Gilfanov · Aug 11, 2014

You can use this tо detect search key press in action bar search:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String s) {
                [You actions here]
                return false;
            }
}