Is there any way in Android to get the height of virtual keyboard of device

To solve this I have written a keyboardHeightProvider which can calculate the height of a floating soft keyboard. The Activity can be set to adjustNone or adjustPan in the AndroidManifest.xml.

https://github.com/siebeprojects/samples-keyboardheight

Siebe


I tried many suggested methods for this, but none seemed to work for Android SDL. I think this is either because the SDL display is "full screen" or that it sits within an "AbsoluteLayout" and therefore the height of the "View" never actually changes. This method worked for me:

Getting the dimensions of the soft keyboard

Window mRootWindow = getWindow();
View mRootView = mRootWindow.getDecorView().findViewById(android.R.id.content);
mRootView.getViewTreeObserver().addOnGlobalLayoutListener(
    new ViewTreeObserver.OnGlobalLayoutListener() {
    public void onGlobalLayout(){
        Rect r = new Rect();
        View view = mRootWindow.getDecorView();
        view.getWindowVisibleDisplayFrame(r);
        // r.left, r.top, r.right, r.bottom
    }
    });

Yes you can, with the help of Viewtree Observer and global layout listener, just try below mentioned steps

  1. Get the root view of your layout
  2. get the Viewtree observer for this root, and add a global layout listener on top of this.

now whenever soft keyboard is displayed android will re-size your screen and you will receive call on your listener. That's it only thing you now need to do is calculate difference between height which your root view has after re-size and original size. If difference is more then 150 consider this as a keyboard has been inflated.

Below is a sample code

root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
     public void onGlobalLayout(){
           int heightDiff = root.getRootView().getHeight()- root.getHeight();
           // IF height diff is more then 150, consider keyboard as visible.  
        }
  });

Regards, Techfist