DEV Community

Cover image for Android SearchView.OnCloseListener Not Working? Here is how to fix it.
K M Rejowan Ahmmed
K M Rejowan Ahmmed

Posted on

2

Android SearchView.OnCloseListener Not Working? Here is how to fix it.

I was working on an android app project which required a SearchView. While working on the SearchView, I discovered that the method setOncloseListener is not working for the SearchView. I did some research and found that a lot of people like are having the same issues. After some testing I found a solution to make the close icon click listener or the OnCloseListener for SearchView work. It is not ideal, but here we go.

SearchView

SearchView with Close Icon

SearchViewSearchView with Close IconBefore going further here are some primary details,

  • I'm using androidx.appcompat.widget.SearchView in a layout.
  • I needed the method to clear queries from searchView, remove focus and finally hide the soft keyboard.

Solution

In your java code add these lines to make the magic work:


 java
        SearchView searchView = findViewById(R.id.search_view);

        int searchCloseButtonId = searchView.findViewById(androidx.appcompat.R.id.search_close_btn).getId();
        ImageView closeButton = searchView.findViewById(searchCloseButtonId);
        closeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchView.setQuery("", false);
                searchView.clearFocus();
                binding.searchResultLayout.setVisibility(GONE);
                hideKeyboard();
            }
        });


Enter fullscreen mode Exit fullscreen mode

for hideKeyboard:


 java
        private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(binding.searchView.getWindowToken(), 0);
        }


Enter fullscreen mode Exit fullscreen mode

And that's it. Now you can make your searchView close icon work as you like.

How it's working?

SearchView has IDs for its own components, in this case, the close icon. In the code have searchView.findViewById(androidx.appcompat.R.id.search_close_btn).getId() which calls the id of the close image from searchView itself.
Later we added this id to an ImageView by using searchView.findViewById(searchCloseButtonId) this. And then just setting on click listener for the ImageView and creating a method for hideKeyboard from searchView windowToken.

So, now you have a solution to fix problems with searchView.setOnCloseListener.

Find me on - Github, Facebook

Sentry mobile image

Improving mobile performance, from slow screens to app start time

Based on our experience working with thousands of mobile developer teams, we developed a mobile monitoring maturity curve.

Read more

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay