Android - Detect visible keyboard?

12,329

Solution 1

I think this thread should answer your question. To summarize, you can give your activity's root view an id, such as "@+id/activityRoot", and then hook a GlobalLayoutListener into the ViewTreeObserver for that view. In the listener is where you check the visibility of the keyboard, like so:

final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  @Override
  public void onGlobalLayout() {
    if (getResources().getConfiguration().keyboardHidden == Configuration.KEYBOARDHIDDEN_NO) { // Check if keyboard is not hidden
     // ... do something here
    }
  }
});

This is a combination of @Reuben_Scratton and @Yogesh's answers in the above thread.

UPDATE: Note that the documentation for keyboardHidden says it will ALWAYS return Configuration.KEYBOARDHIDDEN_YES if there is a hard keyboard available on the device(i.e. like a Motorola Droid 1 & 2)

Solution 2

try this or this workaround since its not possible within "simple" sdk method invocation

Share:
12,329
aryaxt
Author by

aryaxt

Updated on June 09, 2022

Comments

  • aryaxt
    aryaxt over 1 year

    Is it possible to detect whether a keyboard is visible on the screen or not?

    Thanks

  • aryaxt
    aryaxt almost 12 years
    It returns true all the time, even when my keyboard is not visible on the screen
  • Rafael T
    Rafael T almost 12 years
    this will not work if someone is using long-press-menu to trigger the keyboard. It tells you only if a view is active for recieving soft-keyboard events.
  • Bondax
    Bondax about 11 years
    @Pheonixblade9: This does not work, read the docu on isActive() more carefully! It's not about visibility but about the 'active' state.
  • Yi H.
    Yi H. over 10 years
    It doesn't work. It always returns Configuration.KEYBOARDHIDDEN_NO to me (no hard keyboard on my device).