How do I close a SearchView programmatically?

后端 未结 13 1711
误落风尘
误落风尘 2020-12-23 19:10

I currently have a SearchView in the action bar of my app. When I click the search icon, the SearchView expands and the keyboard pops up as expecte

相关标签:
13条回答
  • 2020-12-23 19:28

    To intercept BACK button override onBackPressed() (see docs)

    @Override
    public void onBackPressed() {
    
        if (isSearchViewVisible) {
            SearchView searchView = (SearchView) menu.findItem(R.id.searchBox)
               .getActionView();
    
            // This method does not exist
            searchView.invokeClose();
        } else {
            super.onBackPressed();
        }
    }
    

    EDIT:

    Docs say:

    If necessary, you can expand or collapse the action view in your own code by calling expandActionView() and collapseActionView() on the MenuItem.

    0 讨论(0)
  • 2020-12-23 19:29

    Also you can use collapseActionView. It automatically handle back button, collapsing SearchView

    <item
          android:id="@+id/action_search"
          android:icon="@drawable/ic_magnify"
          android:title="@string/action_title_search"
          app:actionViewClass="android.support.v7.widget.SearchView"
          app:showAsAction="ifRoom|collapseActionView" />
    
    0 讨论(0)
  • 2020-12-23 19:30

    Based on @MarcinOrlowski answer, also you can use:

    @Override
    public void onBackPressed() {
        if (!searchView.isIconified()) {
            searchView.setIconified(true);
        } else {
            super.onBackPressed();
        }
    }
    
    0 讨论(0)
  • 2020-12-23 19:30

    if you have input on your searchView

    mSearchView.setIconified(true);
    

    will only clear the text.

    The correct method to close a searchView is

    mSearchView.onActionViewCollapsed();
    
    0 讨论(0)
  • 2020-12-23 19:34

    this is the only thing that does it for me:

      toolbar.collapseActionView();
    
    0 讨论(0)
  • 2020-12-23 19:36

    No need to use onBackPressed() method for this!! I found the solution, do only as I mentioned below so that the SearchView itself handle the on back button press event.

    Inside your onCreateOptionsMenu() method,

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        MenuItem menuItem = menu.findItem(R.id.action_search);
        SearchView searchview = (SearchView) menuItem.getActionView();
        searchview.setIconifiedByDefault(true);
        searchview.setQueryHint("Enter your keyword");
        searchview.setOnQueryTextListener(this); 
        super.onCreateOptionsMenu(menu, inflater);
    }
    

    In your menu.xml add showAsAction attribute and set its values as "always|collapseActionView"

    <item
        android:id="@+id/action_search_item"
        android:title="Search"
        android:icon="@drawable/ic_search_logo"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"/>
    

    And you're ready to go ;)

    P.S. - I was searching for a solution from a long time and now I found one, hope it helps you all.

    0 讨论(0)
提交回复
热议问题