I wanna call mSearchView.clearFocus() when virtual keyboard is dismissed, how to do that?
My problem is once the SearchView gets focused, it keeps focused, so if I dismissed the virtual keyboard using back button, and I opened an AlertDialog - for example - the virtual keyboard pops up again once I close the AlertDialog as the search view still has the focus, as if it regains focus.
for the SearchView I used:
android:iconifiedByDefault="false"
android:focusable="false"
for the activity holds the SearchView I use:
android:windowSoftInputMode="stateUnspecified|adjustPan"
even if I changed it to the following I get the same problem
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
Eidt 1:
changing
android:iconifiedByDefault="false"
to be
android:iconifiedByDefault="true"
doesn't solve the problem, I get the same result.
Edit 2:
I tried the approach of creating a custom SearchView and to override onKeyPreIme and call clearFocus(), but onKeyPreIme doesn't get called.
public class ModifiedSearchView extends SearchView {
public ModifiedSearchView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onKeyPreIme (int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
clearFocus();
return false;
}
return super.dispatchKeyEvent(event);
}
}