Search code examples
androidsearchview

How to detect a click anywhere on a SearchView


How to detect a click on searchView?

I have tried using setOnClickListener, setOnSearchClickListener, setOnFocusChangedListener, but to no avail.

The problem seems to be only click on textbox. If I click anywhere outside of textbox onClickListener triggers, but not if click inside the textbox.

The problem that I am trying to solve is that I need to close a if user clicks anywhere. But I dont know how to handle the SearchView.


Solution

  • I have managed to find a way to solve my problem. It's a bit hackish, but it works. You have to get the editText from searchView and set OnClickListener to it

        int id = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
        EditText editText = (EditText) searchView.findViewById(id);
        editText.setOnClickListener(listener);