Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Detect visible keyboard?

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

Thanks

like image 956
aryaxt Avatar asked Dec 08 '11 19:12

aryaxt


2 Answers

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)

like image 128
cbradley Avatar answered Oct 29 '22 19:10

cbradley


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

like image 31
Rafael T Avatar answered Oct 29 '22 19:10

Rafael T