How do I detect if software keyboard is visible on Android Device or not?

ID : 10443

viewed : 32

Tags : androidkeyboardandroid-softkeyboardandroid

Top 5 Answer for How do I detect if software keyboard is visible on Android Device or not?

vote vote

90

This works for me. Maybe this is always the best way for all versions.

It would be effective to make a property of keyboard visibility and observe this changes delayed because the onGlobalLayout method calls many times. Also it is good to check the device rotation and windowSoftInputMode is not adjustNothing.

boolean isKeyboardShowing = false; void onKeyboardVisibilityChanged(boolean opened) {     print("keyboard " + opened); }  // ContentView is the root view of the layout of this activity/fragment     contentView.getViewTreeObserver().addOnGlobalLayoutListener(     new ViewTreeObserver.OnGlobalLayoutListener() {     @Override     public void onGlobalLayout() {          Rect r = new Rect();         contentView.getWindowVisibleDisplayFrame(r);         int screenHeight = contentView.getRootView().getHeight();          // r.bottom is the position above soft keypad or device button.         // if keypad is shown, the r.bottom is smaller than that before.         int keypadHeight = screenHeight - r.bottom;          Log.d(TAG, "keypadHeight = " + keypadHeight);          if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.             // keyboard is opened             if (!isKeyboardShowing) {                 isKeyboardShowing = true                 onKeyboardVisibilityChanged(true)             }         }         else {             // keyboard is closed             if (isKeyboardShowing) {                 isKeyboardShowing = false                 onKeyboardVisibilityChanged(false)             }         }     } }); 
vote vote

85

try this:

InputMethodManager imm = (InputMethodManager) getActivity()             .getSystemService(Context.INPUT_METHOD_SERVICE);      if (imm.isAcceptingText()) {         writeToLog("Software Keyboard was shown");     } else {         writeToLog("Software Keyboard was not shown");     } 
vote vote

72

There is no direct way - see http://groups.google.com/group/android-platform/browse_thread/thread/1728f26f2334c060/5e4910f0d9eb898a where Dianne Hackborn from the Android team has replied. However, you can detect it indirectly by checking if the window size changed in #onMeasure. See How to check visibility of software keyboard in Android?.

vote vote

65

I created a simple class that can be used for this: https://github.com/ravindu1024/android-keyboardlistener. Just copy it in to your project and use as follows:

KeyboardUtils.addKeyboardToggleListener(this, new KeyboardUtils.SoftKeyboardToggleListener() {     @Override     public void onToggleSoftKeyboard(boolean isVisible)     {         Log.d("keyboard", "keyboard visible: "+isVisible);     } }); 
vote vote

58

Very Easy

1. Put id on your root view

rootView is just a view pointing to my root view in this case a relative layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"                 android:layout_width="match_parent"                 android:layout_height="match_parent"                 android:id="@+id/addresses_confirm_root_view"                 android:background="@color/WHITE_CLR"> 

2. Initialize your root view in your Activity:

RelativeLayout rootView = (RelativeLayout) findViewById(R.id.addresses_confirm_root_view);

3. Detect if keyboard is opened or closed by using getViewTreeObserver()

    rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {             @Override             public void onGlobalLayout() {                 int heightDiff = rootView.getRootView().getHeight() - rootView.getHeight();                                  if (heightDiff > 100) { // Value should be less than keyboard's height                      Log.e("MyActivity", "keyboard opened");                 } else {                      Log.e("MyActivity", "keyboard closed");                 }             }         }); 

Top 3 video Explaining How do I detect if software keyboard is visible on Android Device or not?

Related QUESTION?