Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling SearchView

I'm trying to disable a SearchView from my Activity. I tried the following code :

    mSearchView.setEnabled(false);
    mSearchView.setFocusable(false);
    mSearchView.setClickable(false);

But it does not work. SearchView can still be clicked and KeyBoard popps up. I want it to become greyed out and unclickable. How can I accomplish this?

like image 672
Binoy Babu Avatar asked Jul 11 '12 15:07

Binoy Babu


2 Answers

None of the stated answers were sufficient for my needs, so I would like to provide another one for anybody in the same situation.

SearchView, has focus, has search and clear button

A SearchView is made up of different Views, which can be - and in this case have to be - addressed individually. If you want your SearchView (support v7) to freeze and grey out in a state like this, not answering to any kind of input, including the search and clear button, you can use:

ImageView clearButton = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
SearchView.SearchAutoComplete searchEditText = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);

clearButton.setEnabled(false);
searchEditText.setEnabled(false);
searchView.setSubmitButtonEnabled(false);

( Besides, I got an issue with deeptis answer searchView.setInputType(InputType.TYPE_NULL): If you disable the SearchView this way and click it afterwards, the system seemingly expects an open keyboard though the keyboard isn't shown. Therefore, the first back button click causes nothing but closing the - not shown or not in fact opened - keyboard. )

like image 88
outta comfort Avatar answered Nov 15 '22 08:11

outta comfort


To disable any view (e.g. SearchView) either set its input-type to none in layout XML or call view.setInputType(InputType.TYPE_NULL) from the Activity.

like image 31
Deepti Avatar answered Nov 15 '22 10:11

Deepti