Disabling SearchView

大城市里の小女人 提交于 2019-12-05 07:38:11

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

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. )

Deepti

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.

You can also hide the searchView completely. You can hide the searchview and the searchicon by doing this:

    searchItem.setVisible(false);
    searchView.setVisibility(View.GONE);

Then you can bring it back by:

    searchItem.setVisible(true);
    searchView.setVisibility(View.VISIBLE);

To Clear SearchView Focus :

searchView.clearFocus();

To Hide SearchView :

searchView.setVisibility(View.GONE);

From @outta comfort's answer, here is my solution:

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!