Search code examples
androidandroid-search

SearchView's onClose listener never runs


Got a SearchView (androidx, not support lib)

I have added its proper listeners like:

@Override
    public boolean onQueryTextSubmit(String query) {
       Toaster.show(this, "onQueryTextSubmit!");
        return true;
    }

    @Override
    public boolean onQueryTextChange(String query) {
       Toaster.show(this, "onQueryTextChange!");
        return true;
    }

    @Override
    public boolean onClose() {
       Toaster.show(this, "onClose!");
       return true;
}

Both onQueryTextSubmit and onQueryTextChange works well. However, when I click on the small X on the right side of the SearchView, the search view does get cleared, but the onClose listener never runs.

In the official docs it says about setOnCloseListener the following:

"Sets a listener to inform when the user closes the SearchView."

I think I use this properly, yet it is not triggered.

Any ideas?


Solution

  • Ended up:

       View searchViewsCloseBtn = searchView.findViewById(R.id.search_close_btn);
            searchViewsCloseBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                  Toaster.show(myContext, "I hate this platform.");
                }
            });
    

    "R.id.search_close_btn" is the actual reference to the SearchView's close btn.

    If you manually set an onClickListener it works.