1

I want to show a search menuitem in my activity and want to expand this search menuitem in searchview when this menuitem will be clicked.

Below is the search menuItem code.

    <item android:id="@+id/action_search"
    android:title="@string/search_view_hint"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:icon="@drawable/ic_action_search"
    android:orderInCategory="200"
    android:visible="false"
    app:showAsAction="always|collapseActionView" />

in onCreateOptionsMenu function

MenuItemCompat.setOnActionExpandListener(menu.findItem(R.id.action_search), new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            return true;
        }
    });

in onOptionsItemSelected

item.expandActionView();
mSearchView = (SearchView) item.getActionView();

But the closeListener is not working on this searchview.

mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
                @Override
                public boolean onClose() {
                    return false;
                }
            });

Please help me how can I get this working.

I have used the below code also, but the close is not working at all.

mSearchView.setIconifiedByDefault(true);

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
Prithniraj Nicyone
  • 5,021
  • 13
  • 52
  • 78
  • Take a look at this link: http://javapapers.com/android/android-searchview-action-bar-tutorial/ – Mehta Apr 14 '16 at 05:43
  • I have already checked this but it does not solve my issue. In my case everything is working fine except oncloseListener on the searchview. Please help me if anyone have any idea here, how to handle this. – Prithniraj Nicyone Apr 14 '16 at 06:24
  • Take a look at this [SO question](http://stackoverflow.com/questions/9327826/searchviews-oncloselistener-doesnt-work): `onClose()` seemingly does not work for anybody, use `onMenuItemActionCollapse()` instead – Bö macht Blau Apr 14 '16 at 06:59
  • But `onMenuItemActionCollapse` get called when we click on backButton of the activity, I want to execute some code when searchView's close button will be called. Please help me if anyone know about it. Thanks a lot in advanced. – Prithniraj Nicyone Apr 14 '16 at 07:42

0 Answers0