How to close SearchView programmatic search?

I currently have a SearchView in the action bar of my application. When I click the search icon, the SearchView expands and the keyboard appears as expected. Pressing the “X” in the SearchView window closes the SearchView as expected. However, when SearchView is activated and I press the back button, my application terminates. This is the correct behavior, but now I'm trying to take a picture with the click of a button and just close the SearchView application (not my application) when SearchView is visible. Is there a way to call SearchView OnCloseListener () programmatically by pressing the back button? For example, something like this:

// On a back button press, if we are currently searching, // close the SearchView. Otherwise, invoke normal back button // behavior. public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if (isSearchViewVisible) { SearchView searchView = (SearchView) menu.findItem(R.id.searchBox) .getActionView(); // This method does not exist searchView.invokeClose(); return true; } } return super.onKeyDown(keyCode, event); } 
+46
android searchview
Jul 06 '13 at 19:15
source share
11 answers

Based on @MarcinOrlowski's answer, you can also use:

 @Override public void onBackPressed() { if (!searchView.isIconified()) { searchView.setIconified(true); } else { super.onBackPressed(); } } 
+97
Jul 22 '13 at 8:02
source share
— -

not used

 searchView.setIconifeid(true) 

I also used MenuItemCompat.collapseActionView

 MenuItemCompat.collapseActionView(menuItem) 
+40
Sep 25 '13 at 11:49
source share

There is an easy way to do this:

 @Override public void onBackPressed() { if (!searchView.isIconified()) { searchView.onActionViewCollapsed(); } else { super.onBackPressed(); } } 

or use:

 @Override public void onBackPressed() { searchView.onActionViewCollapsed(); super.onBackPressed(); } 

This will force searchView to collapse before you click back, then the opposite action is called.

Both solutions will work.

+23
May 05 '15 at 9:15
source share

To intercept the BACK button, override onBackPressed() ( see documents )

 @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

The docs say :

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

+21
Jul 6 '13 at 19:17
source share

this is the only thing that does this for me:

  toolbar.collapseActionView(); 
+16
Jul 02 '16 at 19:51
source share

if you have input in searchView

 mSearchView.setIconified(true); 

will only delete text.

The correct method to close searchView is

 mSearchView.onActionViewCollapsed(); 
+5
Nov 24 '16 at
source share

Using:

 searchView.setIconified(true); 
+2
Jun 12 '15 at 13:24
source share

I prefer ! SearchView.isIconified () on if (isSearchViewVisible) inside the onBackPressed () method, since option 2 does not work when you have fragments added to the backman stack that you want to show when you click the back button.

+1
Sep 11 '13 at 23:45
source share

My way:

  • Create CustomSearchView Class
     public class CustomSearchView extends SearchView {
         public CustomSearchView (final Context context) {
             super (context);
             this.setIconifiedByDefault (true);
         }

         @Override
         public boolean dispatchKeyEventPreIme (KeyEvent event) {
             if (event.getKeyCode () == KeyEvent.KEYCODE_BACK && 
                 event.getAction () == KeyEvent.ACTION_UP) {
                 this.onActionViewCollapsed ();
             }
             return super.dispatchKeyEventPreIme (event);
         }
     }

  1. Add actionViewClass
     <item
         android: id = "@ + id / menu_search"
         android: title = "@ string / menu_search"
         android: icon = "@ drawable / ic_search"
         android: showAsAction = "collapseActionView | ifRoom"
         android: actionViewClass = "com.myapp.CustomSearchView" />
  1. Create CustomSearchView in onCreateOptionsMenu
     CustomSearchView searchView = (CustomSearchView) menu.findItem (R.id.menu_search) .getActionView ();
0
Aug 19 '15 at 4:13
source share

If you are not using any function in the onBackPressed() method, remove it from your Activity.So that SearchView itself SearchView event.

I use SearchView as

 @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { MenuItem searchItem = menu.findItem(R.id.action_search); SearchView searchview = (SearchView) MenuItemCompat.getActionView(searchItem); searchview.setIconifiedByDefault(true); searchview.setOnQueryTextListener(this); searchview.setSubmitButtonEnabled(true); searchview.setQueryHint("Search Here"); super.onCreateOptionsMenu(menu, inflater); } 

and my menu.xml as follows

 <item android:id="@+id/action_search" android:icon="@drawable/search_tool" android:orderInCategory="1" android:title="Search" app:actionViewClass="android.support.v7.widget.SearchView" app:showAsAction="always|collapseActionView"/> 
0
Dec 22 '15 at 9:09
source share

You can also use collapseActionView . It automatically processes the return button, minimizing 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
Oct 02 '16 at 10:54 on
source share



All Articles