I think you might want to turn the things around and instead of detecting visibility of softkeyboard, then take control of it.
The code I am using for this is:
public static void showKeyboard(Activity activity) {
if (activity == null || activity.getCurrentFocus() == null)
return;
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
// will only trigger if no physical keyboard is open
inputMethodManager.showSoftInput(activity.getCurrentFocus(), InputMethodManager.SHOW_IMPLICIT);
}
public static void hideKeyboard(Activity activity) {
if (activity == null || activity.getCurrentFocus() == null)
return;
InputMethodManager imm = (InputMethodManager) activity.getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
I also have a third method for hiding the keyboard. This was needed in a dialog showing an edittext to force the keyboard to close when 'Ok/cancel' is clicked:
public static void hideKeyboard(Context c, IBinder windowToken) {
InputMethodManager mgr = (InputMethodManager) c
.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(windowToken, 0);
}
Usage:
Util.hideKeyboard(getActivity(), editText.getWindowToken());