How do I capture SearchView's clear button click?
After trying a lot of combinations, I found how to capture the event behind the X button in SearchView
Below is a code snippet from onCreateOptionsMenu
function in one of my apps. mSearchMenu
and mSearchView
are global variables.The X is actually an ImageView with ID search_close_btn
and the text area is an EditText view with ID search_src_text
@SuppressLint("NewApi") @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu items for use in the action bar MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.country_list_activity_actions, menu); mSearchMenu = menu.findItem(R.id.action_search); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { // Get the SearchView and set the searchable configuration SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView(); // Assumes current activity is the searchable activity mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); mSearchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default // Get the search close button image view ImageView closeButton = (ImageView)mSearchView.findViewById(R.id.search_close_btn); // Set on click listener closeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { LoggerUtils.d(LOG, "Search close button clicked"); //Find EditText view EditText et = (EditText) findViewById(R.id.search_src_text); //Clear the text from EditText view et.setText(""); //Clear query mSearchView.setQuery("", false); //Collapse the action view mSearchView.onActionViewCollapsed(); //Collapse the search widget mSearchMenu.collapseActionView(); } }); } // When using the support library, the setOnActionExpandListener() method is // static and accepts the MenuItem object as an argument mSearchMenu.setOnActionExpandListener(new OnActionExpandListener() { @Override public boolean onMenuItemActionExpand(MenuItem item) { //Nothing to do here LoggerUtils.d(LOG, "Search widget expand "); return true; // Return true to expand action view } @Override public boolean onMenuItemActionCollapse(MenuItem item) { LoggerUtils.d(LOG, "Search widget colapsed "); return true; // Return true to collapse action view } }); return super.onCreateOptionsMenu(menu); }
you can just use the onCloseListener()
sv= (SearchView) findViewById(R.id.searchView1);sv.setOnCloseListener(new OnCloseListener() { @Override public boolean onClose() { Toast t = Toast.makeText(MainActivity.this, "close", Toast.LENGTH_SHORT); t.show(); return false; } });
I had problems trying to find the component by its ID but I found another way to search this component using the context of the same SearchView
// Catch event on [x] button inside search viewint searchCloseButtonId = searchView.getContext().getResources() .getIdentifier("android:id/search_close_btn", null, null);ImageView closeButton = (ImageView) this.searchView.findViewById(searchCloseButtonId);// Set on click listenercloseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Manage this event. }});