2

Hello Android developer,

We can detect soft keyboard by setting a OnGlobalLayoutListener.

When height of window changes we can do some calculation from that we know keyboard is visible or not. lot of discussion you can find here (link) this will work when we use windowSoftInputMode as adjustResize

In my scenario im using adjustPan as windowSoftInputMode for this, window size won't change when soft keyboard is visible. (Don't tell to change windowSoftInputMode to "adjustResize")

I know we can implement in Context.INPUT_METHOD_SERVICE with isAcceptingText but i can't implement this becasue i have lot of EditView in my app.

I know they dont have any Broadcast receiver for listening soft keyboard visiblity.

Is they any way we can achive/ detect soft keyboard visibility and hide/invisible

Please give your thoughts. Thanks in advance for your answer.

Community
  • 1
  • 1
Perumal
  • 91
  • 6

1 Answers1

0

This worked for me:

Get the soft keyboard height using this method:

public static int getSoftKeyboardHeight(Activity activity, int screenHeight) {
    Rect r = new Rect();
    View rootview = activity.getWindow().getDecorView();
    rootview.getWindowVisibleDisplayFrame(r);
    return screenHeight - r.height();
}

If returned height is 0 that means the keyboard is not visible, else the keyboard is visible. Notice this should be run continuously (when keyboard is suspected to be opened / closed), so I think it's advisable to spawn a separate thread for this check as to not perform extra work on the UI thread.

D.Fonkaz
  • 45
  • 2
  • 6