How to completely collapse a SearchView after an item selected?
Solution 1
Found it. The call should be _searchView.onActionViewCollapsed()
. Why on earth you call a "listener" style method (ie, one that begins with 'on') is beyond me.
Solution 2
Instead of calling _searchView.onActionViewCollapsed()
call menuItem.collapseActionView()
where _searchView = menuItem.getActionView()
.
Solution 3
@sebastian that's not actually right.
I've been stucked at this issue for a while, but finally I've managed to handle it in the right way. You're suppose to call menuSearch.collapseActionView(); instead. This method will call onActionViewCollapsed, which you could override. So you don't call a listener ;)
MenuItem menuSearch = menu.findItem(R.id.itemSearch);
SearchView searchView = (SearchView) menuSearch.getActionView();
//Don't use next line
//searchView.onActionViewCollapsed();
menuSearch.collapseActionView();
Solution 4
If you're using Toolbar (android.support.v7.widget.Toolbar) and the corresponding SearchView (android.support.v7.widget.SearchView) with it, this works:
searchView.setQuery("", false);
searchView.clearFocus();
searchView.setIconified(true);
Solution 5
You should clear the search, remove the focus and then call:
searchView.setIconified(true);
Comments
-
Sebastian over 3 years
I've been struggling with this for weeks.. I have a global search that offers up a custom listview with suggestions as a user types. When a user selects an option, I want the searchview to return to its completely collapsed state.
Instead, it shrinks down but stays in a slightly expanded view.
I've thrown EVERYTHING I can find at this thing to close it, but cannot for the life of me get the right method. Here's the function:
final SearchView.OnCloseListener closeListener = new SearchView.OnCloseListener() { @Override public boolean onClose() { return closeSearch(); } }; protected boolean closeSearch() { _searchView.clearFocus(); _searchView.setQuery("", false); _searchView.setFocusable(false); _searchMenuItem.collapseActionView(); isSearchFragmentVisible(false); return false; }
Close search is then manually called when an item is selected from the custom "suggestion" listview.
-
Nicolas Buquet over 10 yearsI agree with Sebastian : using a searchView from android.support.v7.widget.SearchView in a menuItem from android.support.v4.view.MenuItemCompat in an ActionBar, the searchView cannot be collapsed calling
MenuItemCompat.collapseActionView(searchMenuItem);
I had to callsearchView.onActionViewCollapsed();
in the "onQueryTextSubmit" callback. The keybaord is dismissed too. My keyboard "Ok" button label switches between "Ok" and "Next" between successive invocation of the searchView. I don't know why actually. -
reactive-core almost 10 yearsIt works for API level 14 and above. If using the compat library, you can use MenuItemCompat.collapseActionView(menuItem);
-
wayzz about 9 yearsI am using this method to close the searchview and keyboard. But the thing is then when I reopen it for another search, I can not submit the query because on the keyboard the 'Go' button is changed into a 'Next' button. Do you have a solution to this?
-
Dario Picco over 8 yearsIn my case .collapseActionView() using android support v7 widget SearchView doesn't work. Instead calling onActionViewCollapred() work as exprected..
-
Analizer over 8 yearsonly @Ender Muab'Dib 's answer worked for me, you should call menuSearch.collapseActionView()
-
Louis CAD over 7 yearsOnly this worked for me, using AppCompat with Toolbar and support SearchView. Should be the accepted answer
-
AlxDroidDev over 7 yearsPerfect! Visually it is way better than the alternative. Plus the menuItem object is often visible from within the caller, which the searchView usually isn't (I am basing these statements on the code that is usually around).
-
sunil over 6 yearsThis is because, first call will clear the text and the second call closes the searchview. See the implementation here: blog.http417.com/2014/07/… or android.googlesource.com/platform/frameworks/support.git/+/… (search for setIconified method)
public void setIconified(boolean iconify) { if (iconify) { onCloseClicked(); } else { onSearchClicked(); } }