How to completely collapse a SearchView after an item selected?

36,028

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);
Share:
36,028
Sebastian
Author by

Sebastian

Developer based out of NYC.

Updated on August 27, 2020

Comments

  • Sebastian
    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.

    enter image description here

    Instead, it shrinks down but stays in a slightly expanded view.

    enter image description here

    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
    Nicolas Buquet over 10 years
    I 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 call searchView.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
    reactive-core almost 10 years
    It works for API level 14 and above. If using the compat library, you can use MenuItemCompat.collapseActionView(menuItem);
  • wayzz
    wayzz about 9 years
    I 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
    Dario Picco over 8 years
    In my case .collapseActionView() using android support v7 widget SearchView doesn't work. Instead calling onActionViewCollapred() work as exprected..
  • Analizer
    Analizer over 8 years
    only @Ender Muab'Dib 's answer worked for me, you should call menuSearch.collapseActionView()
  • Louis CAD
    Louis CAD over 7 years
    Only this worked for me, using AppCompat with Toolbar and support SearchView. Should be the accepted answer
  • AlxDroidDev
    AlxDroidDev over 7 years
    Perfect! 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
    sunil over 6 years
    This 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(); } }