How to detect SearchView's back button press?
So looking at the title of your question, you're not sure how to detect when the SearchView
is being closed and hence you're not able to restore your views to the "normal" state when the SearchView
is not open anymore.
Outdated solution as of Android 4.0+
The (simple) solution is to add a: SearchView.OnCloseListener
to your SearchView
, like this:
SearchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
// This is where you can be notified when the `SearchView` is closed
// and change your views you see fit.
}
});
EDIT:
Updated solution that works in Android 4.0+
Apparently the OnCloseListener
is buggy and doesn't work on newer versions of Android (4.0+). See: https://code.google.com/p/android/issues/detail?id=25758
The solution to this is to use the Support library version of the SearchView
:
My onCreateOptionsMenu
looks like this:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
MenuItem searchMenuItem = menu.findItem(R.id.searchView);
SearchView searchView = (SearchView) searchMenuItem.getActionView();
MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
Toast.makeText(ScrollingActivity.this, "onMenuItemActionExpand called", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
Toast.makeText(ScrollingActivity.this, "onMenutItemActionCollapse called", Toast.LENGTH_SHORT).show();
return true;
}
});
}
As to why Google didn't even bothered to write a comment in the documentation I don't know, but it's sad.
EDIT2:
Just adding the link which @MateiRadu added in the comments:
how to handle back button of Search View in android
This also shows how to use the OnActionExpandListener
instead of the OnCloseListener
.
Hope this helps.
Darwind's answer will do the job but that only works when user presses the back button to close the keyboard and when SearchView
doesn't have focus anymore.
The better way to do it is to listen for SearchView
's text changes.
This way you can restore views and control UI as the text is updating.
here's how I do it.
SearchView searchView = new SearchView(context);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
// this is when user is done typing and clicks search
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// you can use this to do a "live" search while the user is typing.
// this will trigger each time user changes (adds or removes) text
// so when newText is empty, restore your views
return false;
}
});
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean newViewFocus)
{
if (!newViewFocus)
{
//Collapse the action item.
searchItem.collapseActionView();
//Clear the filter/search query.
myFilterFunction("");
}
}
});
return super.onCreateOptionsMenu(menu);
}