SearchView's OnCloseListener doesn't work

android custom searchview example
searchview android example recyclerview
searchview androidx
searchview android example listview
searchview in actionbar android example
android searchview hint
android searchview suggestions example
actionviewclass searchview

I'm trying to add support for the SearchView in the Android 3.0+ ActionBar, but I can't get the OnCloseListener to work.

Here's my code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    searchView = (SearchView) menu.findItem(R.id.search_textbox).getActionView();
    searchView.setOnQueryTextListener(new OnQueryTextListener() {
        @Override
        public boolean onQueryTextChange(String newText) {
            searchLibrary(newText);
            return false;
        }
        @Override
        public boolean onQueryTextSubmit(String query) { return false; }
    });
    searchView.setOnCloseListener(new OnCloseListener() {
        @Override
        public boolean onClose() {
            System.out.println("Testing. 1, 2, 3...");
            return false;
        }
    });
    return true;
}

The search works great and every is working except for the OnCloseListener. Nothing is being printed to Logcat. Here's the Logcat for when I'm pressing the "Close" button:

02-17 13:01:52.914: I/TextType(446): TextType = 0x0
02-17 13:01:57.344: I/TextType(446): TextType = 0x0
02-17 13:02:02.944: I/TextType(446): TextType = 0x0

I've looked through the documentation and samples, but nothing seemed to change it. I'm running it on a Asus Transformer Prime and a Galaxy Nexus, both on Ice Cream Sandwich. Any ideas?

Update:

Yes - System.out.println() does work. Here's proof:

   @Override
 public boolean onQueryTextChange(String newText) {
    System.out.println(newText + "hello");
    searchLibrary(newText);
    return false;
 }

Results in this Logcat:

02-17 13:04:20.094: I/System.out(21152): hello
02-17 13:04:24.914: I/System.out(21152): thello
02-17 13:04:25.394: I/System.out(21152): tehello
02-17 13:04:25.784: I/System.out(21152): teshello
02-17 13:04:26.064: I/System.out(21152): testhello

I also meet this problem, and I have no choice but give up "oncloselistener". Instead, you can get your menuItem, then setOnActionExpandListener. Then override unimplents methods.

@Override
public boolean onMenuItemActionExpand(MenuItem item) {
    // TODO Auto-generated method stub
    Log.d("*******","onMenuItemActionExpand");
    return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
    //do what you want to when close the sesarchview
    //remember to return true;
    Log.d("*******","onMenuItemActionCollapse");
    return true;
}

Creating a Search Interface, Android SearchView with Example. Search View widget is used to provide a search interface to user so that the user can enter his search query and submit a​  Android SearchView provides user interface to search query submitted over search provider. SearchView widget can be implemented over ToolBar/ActionBar or inside a layout. SearchView is by default collapsible and set to be iconified using setIconifiedByDefault(true) method of SearchView class.

For Android API 14+ (ICS and greater) use this code:

// When using the support library, the setOnActionExpandListener() method is
// static and accepts the MenuItem object as an argument
MenuItemCompat.setOnActionExpandListener(menuItem, new OnActionExpandListener() {
    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        // Do something when collapsed
        return true;  // Return true to collapse action view
    }

    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        // Do something when expanded
        return true;  // Return true to expand action view
    }
});

For more information: http://developer.android.com/guide/topics/ui/actionbar.html#ActionView

Ref: onActionCollapse/onActionExpand

SearchView Tutorial With Example In Android Studio, Android SearchView Example Tutorial. Android Search View widget to add search bar in your android application. Android Search ToolBar or ActionBar  In Android, SearchView widget provide search user interface where users can enter a search query and then submit a request to search provider. It shows a list of query suggestions or results if available and allow the users to pick a suggestion or result to launch into. Basic SearchView code in XML:

For this problem I came up with something like this,

private SearchView mSearchView;

@TargetApi(14)
@Override
public boolean onCreateOptionsMenu(Menu menu)
{

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.conversation_index_activity_menu, menu);

    mSearchView = (SearchView) menu.findItem(R.id.itemSearch).getActionView();

    MenuItem menuItem = menu.findItem(R.id.itemSearch);

    int currentapiVersion = android.os.Build.VERSION.SDK_INT;
    if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    {
        menuItem.setOnActionExpandListener(new OnActionExpandListener()
        {

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item)
            {
                // Do something when collapsed
                Log.i(TAG, "onMenuItemActionCollapse " + item.getItemId());
                return true; // Return true to collapse action view
            }

            @Override
            public boolean onMenuItemActionExpand(MenuItem item)
            {
                // TODO Auto-generated method stub
                Log.i(TAG, "onMenuItemActionExpand " + item.getItemId());
                return true;
            }
        });
    } else
    {
        // do something for phones running an SDK before froyo
        mSearchView.setOnCloseListener(new OnCloseListener()
        {

            @Override
            public boolean onClose()
            {
                Log.i(TAG, "mSearchView on close ");
                // TODO Auto-generated method stub
                return false;
            }
        });
    }


    return super.onCreateOptionsMenu(menu);

}

Android SearchView with Example, Android SearchView provides user interface to search query submitted over search provider. SearchView widget can be implemented over ToolBar/ActionBar or  SearchView in Android is a very sought after View. You cannot find any website nowadays, without a search bar. The same applies to an application also. There are a lot of ways to auto-populate a search query on entering a key phrase into the SearchView.

I ran into same problem on android 4.1.1. Looks like it is a known bug: https://code.google.com/p/android/issues/detail?id=25758

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() {

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

    @Override
    public void onViewAttachedToWindow(View arg0) {
        // search was opened
    }
});

Above code worked well in my case.


I post same answer here: https://stackoverflow.com/a/24573266/2162924

Android SearchView Example Tutorial using DataBinding , This example demonstrates how do I use SearchView in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and  Android SearchView Android allows us to use the search functionality in our app by displaying the SearchView widget either in the ToolBar/ActionBar or inserting it into a layout. Android SearchView widget is available from Android 3.0 onwards. The SearchView is defined in the XML layout as shown below.

I ended up using a bit of a hack, that works well for my purpose - not sure it'll work with all purposes. Anyway, I'm doing a check to see if the search query is empty. This is not really related to the SearchView's OnCloseListener though - that still doesn't work!

searchView.setOnQueryTextListener(new OnQueryTextListener() {
            @Override
            public boolean onQueryTextChange(String newText) {
                if (newText.length() > 0) {
                    // Search
                } else {
                    // Do something when there's no input
                }
                return false;
            }
            @Override
            public boolean onQueryTextSubmit(String query) { return false; }
        });

Providing Search with SearchView (Android Development Patterns , This example demonstrates how do I use searchView in Toolbar android.Step 1 − Create a new project in Android Studio, go to File⇒ New  In this video we will learn how to use the SearchView widget to filter a RecyclerView in real time. For this we will add the SearchView as an item to our options menu and make it expandable with the collapseActionView attribute. This way we can display it as an icon in our app bar, which expands to an input field when we click on it.

How to Filter a RecyclerView with SearchView, In the folder res/menu create a new file called main_menu.xml. In it add an item and set the actionViewClass to android.support.v7.widget.SearchView. Since you are using the support library you have to use the namespace of the support library to set the actionViewClass attribute. Your xml file should look something like this:

Android SearchView, Hello developers, In today’s tutorial, we will learn about listview with searchview android studio example. In the scenarios where there are many items showed with listview, we need to put search filter functionality. It let the user find his desired information quickly and easily.

How to use SearchView in Android?, Add MaterialSearchView to your layout file along with the Toolbar (Add this block at the bottom of your layout, in order to display it over the rest of the view): <!— Must be last for right layering display —> < FrameLayout android:id = "@+id/toolbar_container" android:layout_width = "match_parent" android:layout_height = "wrap_content

Comments
  • Hmm, Works OK for me with Android 3.2 but NOT for 4.0+
  • Raised bug, 25758
  • I'm glad it is not just me having this problem. Anyone have any other hacks other than the one below?
  • I have learnt two things if showAsAction is set to always. Search box has a close button of its own but if it is set to ifRoom | collapseActionView it expands on the action bar.
  • I believe we're only talking about ActionBar's SearchView which is only honeycomb+
  • don't bother using onCloseListener, just use this one with your menu item.
  • I think you can use MenuItemCompat.OnActionExpandListener for earlier API levels: developer.android.com/reference/android/support/v4/view/…
  • Full answer: if (Build.VERSION.SdkInt > BuildVersionCodes.NMr1) item.SetOnActionExpandListener(this); else MenuItemCompat.SetOnActionExpandListener(item, this);
  • What happens if you have always expanded using setIconofiedByDefault(false) ? It dosen't works... :(
  • Yes, that bug was created right after I posted this question. See the comments on the original question.