Can you help me on how to disable searchview when button is pressed? I\'m trying this code:
searchView.setEnabled(false);
searchView.setFocu
Did you try
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);
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
.
searchView.setOnQueryTextFocusChangeListener( new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus && !v.isEnabled()) v.clearFocus();
}
}