Issue
Is there any way in Android to get the height of the virtual keyboard displayed on the device in run time? Actually I want to show a text box above the keyboard.
Solution
Yes you can, with the help of Viewtree Observer and global layout listener, just try below mentioned steps
- Get the root view of your layout
- 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
Answered By - Techfist
Answer Checked By - Mildred Charles (JavaFixing Admin)