SearchView.OnCloseListener does not get invoked

Im using an action bar and adding a searchView to it. I have implemented the searchView.onCLoseListener but this does not seem to be getting invoked. Any suggestions ?

My code looks something like this :

SearchView searchView = new SearchView(getContext());
searchView.setOnCloseListener(new OnCloseListener() {

  public boolean onClose() {
    // Do Something

    return true;


Instead of searchView.setVisibility(SearchView.GONE); replace it with searchView.setVisibility(View.GONE);.

And if you want the searchView to go invisible instead of disappearing you can use searchView.setVisibility(View.INVISIBLE).

Ok. i got the mistake. We cant add a searchCommand and do


Doing this would remove the searchText if any and hence we cant do a onClose().

What I did to sort out a similar problem is I created a new class that extended SearchView:

public class EnglishVerbSearchView extends SearchView {

OnSearchViewCollapsedEventListener mSearchViewCollapsedEventListener;

public EnglishVerbSearchView(Context context) {

public void onActionViewCollapsed() {
    if (mSearchViewCollapsedEventListener != null)

public interface OnSearchViewCollapsedEventListener{
    public void onSearchViewCollapsed();

public void setOnSearchViewCollapsedEventListener(OnSearchViewCollapsedEventListener eventListener) {
    mSearchViewCollapsedEventListener = eventListener;


You can then use this class instead of SearchView in your menu xml file:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android=""
      xmlns:yourapp="" >
    <item android:id="@+id/action_search"
      yourapp:actionViewClass="com.szymontrapp.englishverbs.EnglishVerbSearchView" />

And then you can add a listener in your activity:

    getMenuInflater().inflate(, menu);
    MenuItem searchItem = menu.findItem(;
    EnglishVerbSearchView searchView = (EnglishVerbSearchView)      MenuItemCompat.getActionView(searchItem);       

    // Get the SearchView and set the searchable configuration
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    // Assumes current activity is the searchable activity
    searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default


You can also override other methods in your class that replaces SearchView to achieve other goals.

I ran into same problem on android 4.1.1. Looks like it is a known bug:

Anyway, as a workaround i used state change listener (when SearchView is detached from action bar, it is also closed obviously).

view.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {

    public void onViewDetachedFromWindow(View arg0) {
        // search was detached/closed

    public void onViewAttachedToWindow(View arg0) {
        // search was opened

Above code worked well in my case.