8

Can you help me on how to disable searchview when button is pressed? I'm trying this code:

searchView.setEnabled(false);
                searchView.setFocusableInTouchMode(false);
                searchView.clearFocus();

but it seems not working. I can still input text in searchview.

Thanks.. :))

Theresa Gamit
  • 292
  • 1
  • 4
  • 14

11 Answers11

14

All above questions don't work for me. Becase SearchView is a ViewGroup, so we have to disable all its child views.

private void enableSearchView(View view, boolean enabled) {
    view.setEnabled(enabled);
    if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) view;
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View child = viewGroup.getChildAt(i);
            enableSearchView(child, enabled);
        }
    }
}

In other place, call this:

enableSearchView(searchView, true/false);
Cuong Nguyen
  • 1,166
  • 1
  • 11
  • 20
9

You can use:

searchView.clearFocus();

and if you want to hide it using:

searchView.setVisibility(View.GONE);
Karla
  • 118
  • 8
3

Try the following:

searchview.setInputType(InputType.TYPE_NULL);
keyser
  • 18,829
  • 16
  • 59
  • 101
NehaC
  • 189
  • 1
  • 1
  • 5
2

SearchView isn't the actual EditText you type into, but rather is a LinearLayout which holds a bunch of views, including the EditText.

To get the view you actually want for disabling it:

EditText searchViewEditText = (EditText) searchView.findViewById(R.id.search_src_text);

Note, this only works if you're using support v7 SearchView, since the particular resource id is internal if you use the framework SearchView.

Dinith Rukshan Kumara
  • 638
  • 2
  • 10
  • 19
David Liu
  • 9,426
  • 5
  • 40
  • 63
1
searchView.findViewById(R.id.search_button).setEnabled(false);

But you must check it for null and hide searchView:

ImageView searchBtn = (ImageView) searchView.findViewById(R.id.search_button);
if (searchBtn != null) searchBtn.setEnabled(false);
searchView.setVisibility(View.GONE);

I think this will work

Shwarz Andrei
  • 647
  • 14
  • 17
0

Did you try

searchView.setVisibility(View.GONE); ?

Paul
  • 5,163
  • 3
  • 35
  • 43
0

This worked for me

ImageView searchIcon = (ImageView)searchView.findViewById(android.support.v7.appcompat.R.id.search_button);
    searchIcon.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           // Don't perform any action.
        }
    });
Bharat Kul Ratan
  • 985
  • 2
  • 12
  • 24
0

Try below code:

searchView.setIconified(true);
Dinith Rukshan Kumara
  • 638
  • 2
  • 10
  • 19
Kishan Gohel
  • 453
  • 4
  • 12
  • if you are using android.support.v7.widget.SearchView, to close searchview first call of setIconified, text is cleared, second call of setIconified, keyboard and search view get closed. – Kishan Gohel Feb 28 '17 at 06:57
  • Edit your answer and add any description to it so that it will help others to understand the reason why it will work. – VPK Feb 28 '17 at 06:59
0
searchView.setOnQueryTextFocusChangeListener( new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus && !v.isEnabled()) v.clearFocus();
            }
        }
  • 1
    Although this might answer the question, it is always good to describe why/how the answer will provide a solution to the problem. – Reg Mar 06 '18 at 14:21
0
val searchEditText: EditText =
        searchView.findViewById(androidx.appcompat.R.id.search_src_text)
    searchEditText.isEnabled=false

This will work

-1

If you want to clear it (SearchView), do:

searchView.clearFocus();

and if you want to hide it temporarily, do:

searchView.setVisibility(View.GONE);

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132