- Home /
TouchScreenKeyboard.visible is always false on Android
I am using a WebView plugin to display some web page, and the user can input some text on it. I want to know if the on-screen keyboard is visible or not. I use this static variable,
TouchScreen.Keyboard.visible http://docs.unity3d.com/Documentation/ScriptReference/TouchScreenKeyboard-visible.html
On iOS5, it works perfectly, but on Android (tested on Galaxy S2) it always returns false. The Rect area is also always zero :(
I am using Unity 4.1.2. Is there any workaround for this? Do I need to write a native plugin myself?
Answer by david.gavilan · Jun 14, 2013 at 04:36 AM
In the end, I wrote a native implementation inside the WebView plugin, because it seems Unity can't know when the keyboard has been opened from the WebView.
final View activityRootView = a.getWindow().getDecorView().getRootView();
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new android.view.ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
android.graphics.Rect r = new android.graphics.Rect();
//r will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(r);
android.view.Display display = a.getWindowManager().getDefaultDisplay();
int screenHeight = display.getHeight();
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
//System.out.print(String.format("[NativeWebview] %d, %d\n", screenHeight, heightDiff));
if (heightDiff > screenHeight/3) { // assume that this means that the keyboard is on
UnityPlayer.UnitySendMessage(gameObject, "SetKeyboardVisible", "true");
} else {
UnityPlayer.UnitySendMessage(gameObject, "SetKeyboardVisible", "false");
}
}
});
This is the link to the fix for that plugin: https://github.com/gree/unity-webview/pull/14
Your answer
Follow this Question
Related Questions
[Android] Keyboard doesn't show immediately when selecting inputfield as S Pan. 1 Answer
setting iPhoneKeyboard.text (or any workaround about resetting user input) in Android. 0 Answers
Android Keyboard Issues 0 Answers
Input Field problem (Unity 4.6 UI) on LG G3 Android device 1 Answer
Default Android Keyboard. 0 Answers