B4A Library ABKeyboardVisible

alwaysbusy

Expert
Licensed User
This is a small library I've used in one of our projects where the activity is full screen and I needed to know if the soft keyboard was open or closed.

The lib code is very simple, so I'm even wondering if this couldn't been done using inline Java in B4A (maybe someone has an idea how?)

This is the source code of the lib for those interested in the Java part:

B4X:
package com.ab.abkeyboardvisible;

import android.graphics.Rect;
import android.view.View;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;

@ShortName("ABKeyboardVisible")
@Version(1.00f)
public class ABKeyboardVisible {
   public boolean IsVisible(View decorView, boolean doLog) {
     Rect rect = new Rect();
     decorView.getWindowVisibleDisplayFrame(rect);
     int displayHight = rect.bottom - rect.top;
     int height = decorView.getHeight();
     boolean hide = (double)displayHight / height > 0.8 ;
     if (doLog) {
       BA.Log("DecorView display hight = "+displayHight);
       BA.Log("DecorView hight = "+ height);
       BA.Log("softkeyboard visible = " + !hide);
     }
     return !hide;
   }
}
Usage:
B4X:
' first we need the DecorView of the activity
Dim jo As JavaObject = activity
Dim DecorView As View = jo.RunMethod("getRootView", Null)
If DecorView <> Null Then
     ' check if the keyboard is visible
     Dim keybVis As ABKeyboardVisible
     Dim KeyboardVisible As Boolean = keybVis.IsVisible(DecorView, False)
     If KeyboardVisible Then
        myIME.HideKeyboard
     end if
end if
Attached is the library.
 

Attachments

Top