0

I am using SearchView in the menu of my toolbar within a fragment:

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_songbook_filter, menu);
    MenuItem searchMenuItem = menu.findItem(R.id.action_search);
    mSearchView = (SearchView) searchMenuItem.getActionView();
    mSearchView.setOnQueryTextListener(this);
    mSearchView.setIconified(true);
    mSearchView.setIconifiedByDefault(true);
    mSearchView.onActionViewExpanded();
    mSearchView.setOnCloseListener(new SearchView.OnCloseListener() {
        @Override
        public boolean onClose() {
            // Never gets called
            return false;
        }
    });
}

This is my menu item:

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_search"
        android:title="@string/action_search"
        app:actionViewClass="androidx.appcompat.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView" />

I can perfectly receive setOnQueryTextListener - no problem at all. But I cannot receive the OnClose. I have tried to switch from androidx.appcompat.widget.SearchView to android.widget.SearchView but it does not work either. I have also tried onActionViewCollapsed and onActionViewExpanded which also do not work. This solution does not work either. Does anyone know how I can receive the OnClose?

to_sam
  • 695
  • 8
  • 19

0 Answers0