How to detect a click anywhere on a SearchView

4.4k Views Asked by At

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.

4

There are 4 best solutions below

4
On BEST ANSWER

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);
0
On

Try adding app:iconifiedByDefault="false" to the SearchView tag in your layout XML.

0
On

Try this:

searchView = (SearchView)findViewById(R.id.searchView);
searchView.setOnClickListener(new View.OnClickListener() {
         @Override
     public void onClick(View v) {
         searchView.setIconified(false);
     }
 });
0
On

An answer from a similar question: How to detect if SearchView is expanded?

searchView.setOnQueryTextFocusChangeListener { _ , hasFocus ->
    if (hasFocus) {
        // searchView expanded
    } else {
        // searchView not expanded
    }
}