1

Anyone using Unity3D has tried to solve the problem that TouchScreenKeyboard.area isn't supported on Android devices? This is because for Android you can't get the size of the soft keyboard. By looking into the issues deeper, I figure out that you have to write your own java code for this. Then the problem is there is no native interfaces for this.

My question is: what's the best way to get the area(size) of the TouchScreenKeyboard on Android devices?

Steven
  • 166,672
  • 24
  • 332
  • 435
Raven Zuo
  • 29
  • 3
  • This can be useful, ate least for height: https://stackoverflow.com/questions/35072522/how-to-resize-view-when-touchscreen-keyboard-is-visible-on-android-with-unity – mayo Jun 13 '17 at 18:21

1 Answers1

0
private float GetKeyboardHeight()
{
    #if UNITY_EDITOR
    return 0f; // fake TouchScreenKeyboard height for debug in editor        
    #elif UNITY_ANDROID
    using (AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    {
        AndroidJavaObject View = UnityClass.GetStatic<AndroidJavaObject>("currentActivity").Get<AndroidJavaObject>("mUnityPlayer").Call<AndroidJavaObject>("getView");
        using (AndroidJavaObject rect = new AndroidJavaObject("android.graphics.Rect"))
        {
            View.Call("getWindowVisibleDisplayFrame", rect);
            return (float)(Screen.height - rect.Call<int>("height"));
        }
    }
    #elif UNITY_IOS
    return (float)TouchScreenKeyboard.area.height;
    #else
    return 0f;
    #endif
}
Ardito ITA
  • 19
  • 4