How to disable searchview?

女生的网名这么多〃 提交于 2019-11-29 14:12:26

You can use:

searchView.clearFocus();

and if you want to hide it using:

searchView.setVisibility(View.GONE);

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);
NehaC

Try the following:

searchview.setInputType(InputType.TYPE_NULL);
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

David Liu

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.

Did you try

searchView.setVisibility(View.GONE); ?

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.
        }
    });
searchView.setOnQueryTextFocusChangeListener( new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus && !v.isEnabled()) v.clearFocus();
            }
        }
Kishan Gohel

Try below code:

searchView.setIconified(true);

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

searchView.clearFocus();

and if you want to hide it temporarily, do:

searchView.setVisibility(View.GONE);

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