31

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.

Sebastian
  • 2,470
  • 4
  • 21
  • 27
  • searchMenuItem.collapseActionView(); Thanks To [Newinjava](http://stackoverflow.com/questions/23928253/how-to-dismiss-close-collapse-searchview-in-actionbar-in-mainactivity?rq=1) – Nicks Mar 22 '17 at 11:48

10 Answers10

53

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

siavach
  • 813
  • 9
  • 10
  • 11
    It works for API level 14 and above. If using the compat library, you can use MenuItemCompat.collapseActionView(menuItem); – reactive-core Jun 13 '14 at 22:18
  • 1
    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? – wayzz Mar 16 '15 at 10:46
  • 3
    In my case .collapseActionView() using android support v7 widget SearchView doesn't work. Instead calling onActionViewCollapred() work as exprected.. – Dario Picco Nov 04 '15 at 16:22
  • 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). – AlxDroidDev Jan 10 '17 at 20:29
48

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.

Sebastian
  • 2,470
  • 4
  • 21
  • 27
  • 2
    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. – Nicolas Buquet Dec 20 '13 at 16:56
  • only @Ender Muab'Dib 's answer worked for me, you should call menuSearch.collapseActionView() – Analizer Jan 06 '16 at 08:55
20

@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();
rolgalan
  • 1,631
  • 17
  • 34
13

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);
Helen Hakobyan
  • 734
  • 1
  • 8
  • 18
  • 1
    Only this worked for me, using AppCompat with Toolbar and support SearchView. Should be the accepted answer – Louis CAD Sep 26 '16 at 09:14
8

You should clear the search, remove the focus and then call:

searchView.setIconified(true);
Stephane Maarek
  • 5,202
  • 9
  • 46
  • 87
4
searchView.setIconified(true); // Call this twice
searchView.setIconified(true);

It works for me.

LunaElf
  • 345
  • 3
  • 6
  • 1
    This is because, first call will clear the text and the second call closes the searchview. See the implementation here: http://blog.http417.com/2014/07/how-to-close-searchview-programatically.html or https://android.googlesource.com/platform/frameworks/support.git/+/jb-mr2-release/v7/appcompat/src/android/support/v7/widget/SearchView.java (search for setIconified method) `public void setIconified(boolean iconify) { if (iconify) { onCloseClicked(); } else { onSearchClicked(); } }` – sunil Aug 21 '17 at 14:52
3

This worked for me and it also closed the keyboard by Default

MenuItemCompat.collapseActionView(menuItem);
Bala Vishnu
  • 2,588
  • 23
  • 20
3

This worked for me searchMenuItem.collapseActionView();

inkedTechie
  • 684
  • 5
  • 13
2

Returning false should have been sufficient based on the latest documentation and the source code iff iconifiedByDefault == true.

Did you by any chance define iconifiedByDefault="false" or called setIconifiedByDefault(false)?

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254
0

I've changed the 'Go' button to 'Next', so the only method that worked for me (on API 19) is typing closeBtn.performClick(); closeBtn.performClick(); (Clicking 'close' button twice) in the OnEditorActionListener of the AutoCompleteTextView. You can get the AutoCompleteTextView like this:

LinearLayout linearLayout1 = (LinearLayout) searchView.getChildAt(0);
LinearLayout linearLayout2 = (LinearLayout) linearLayout1.getChildAt(2);
LinearLayout linearLayout3 = (LinearLayout) linearLayout2.getChildAt(1);
AutoCompleteTextView autoComplete = (AutoCompleteTextView) linearLayout3.getChildAt(0);