How to completely collapse a SearchView after an item selected?

Sebastian picture Sebastian · Nov 28, 2012 · Viewed 34.5k times · Source

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.

Answer

siavach picture siavach · Feb 6, 2013

Instead of calling _searchView.onActionViewCollapsed() call menuItem.collapseActionView() where _searchView = menuItem.getActionView().