Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 258
ak7483 Avatar asked Jun 27 '15 22:06

ak7483


2 Answers

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);
like image 102
ak7483 Avatar answered Nov 06 '22 15:11

ak7483


Try this:

searchView = (SearchView)findViewById(R.id.searchView);
searchView.setOnClickListener(new View.OnClickListener() {
         @Override
     public void onClick(View v) {
         searchView.setIconified(false);
     }
 });
like image 33
prashant pol Avatar answered Nov 06 '22 15:11

prashant pol