Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Keyboard height

Discussion in 'Android' started by Edgaras-Randis, Jan 14, 2015.

  1. Edgaras-Randis

    Edgaras-Randis

    Joined:
    Jul 8, 2014
    Posts:
    20
    So, TouchScreenKeyboard.area returns zero rect, anyone has a solution or plugin? Seems like an old issue.
     
  2. Edgaras-Randis

    Edgaras-Randis

    Joined:
    Jul 8, 2014
    Posts:
    20
    Ok, so this is what I got so far, works atm and it's enought for now:


    Code (CSharp):
    1. using(AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    2.         {
    3.             AndroidJavaObject View = UnityClass.GetStatic<AndroidJavaObject>("currentActivity").Get<AndroidJavaObject>("mUnityPlayer").Call<AndroidJavaObject>("getView");
    4.          
    5.             using(AndroidJavaObject Rct = new AndroidJavaObject("android.graphics.Rect"))
    6.             {
    7.                 View.Call("getWindowVisibleDisplayFrame", Rct);
    8.              
    9.                 return Screen.height - Rct.Call<int>("height");
    10.             }
    11.         }
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,431
    I know this is an old thread. But its a life saver. Thanks.
     
  4. rocker182

    rocker182

    Joined:
    Jun 3, 2016
    Posts:
    2
    Can anyone elaborate on how to use this thread
     
  5. Gorlog

    Gorlog

    Joined:
    Jan 20, 2011
    Posts:
    200
    Code (CSharp):
    1.   public int GetKeyboardSize()
    2.     {
    3.         using (AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    4.         {
    5.             AndroidJavaObject View = UnityClass.GetStatic<AndroidJavaObject>("currentActivity").Get<AndroidJavaObject>("mUnityPlayer").Call<AndroidJavaObject>("getView");
    6.  
    7.             using (AndroidJavaObject Rct = new AndroidJavaObject("android.graphics.Rect"))
    8.             {
    9.                 View.Call("getWindowVisibleDisplayFrame", Rct);
    10.  
    11.                 return Screen.height - Rct.Call<int>("height");
    12.             }
    13.         }
    14.     }
     
  6. aschuetzer

    aschuetzer

    Joined:
    Nov 21, 2016
    Posts:
    3
    The Code above is very helpful, but it returns only the height of keyboard, so if there an input field it will be ignored. Have someone an idea how to get the size of the additional input field?
     
  7. benbenmushi

    benbenmushi

    Joined:
    Jan 29, 2013
    Posts:
    20
    In complement to this solution, which work fine on Android.

    Has anyone found a similiar solution for iOS devices ?
     
  8. moghes

    moghes

    Joined:
    Oct 10, 2012
    Posts:
    14
    1. calling this function when the keyboard is visible but still getting 0 as a result.
      Has anyone experienced this?
     
  9. bluescrn

    bluescrn

    Joined:
    Feb 25, 2013
    Posts:
    478
    This code is all over the internet, but the returned value doesn't include the height of the text input box.

    Has anyone figured out how to solve that one? (without just adding a hacky constant to make it almost-work?)
     
    andersemil likes this.
  10. QuincyC

    QuincyC

    Joined:
    Jun 5, 2018
    Posts:
    5
    bump, I'd love to know this as well
     
    andersemil likes this.
  11. ProMaXoid

    ProMaXoid

    Joined:
    Jun 22, 2013
    Posts:
    3
    Code (CSharp):
    1. public static float GetKeyboardHeightRatio()
    2.     {
    3.         if (Application.isEditor)
    4.             return 0.4f;
    5.        
    6. #if UNITY_ANDROID
    7.         using (var unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
    8.         {
    9.             var view = unityClass.GetStatic<AndroidJavaObject>("currentActivity")
    10.                 .Get<AndroidJavaObject>("mUnityPlayer")
    11.                 .Call<AndroidJavaObject>("getView");
    12.            
    13.             var dialog = unityClass.GetStatic<AndroidJavaObject>("currentActivity")
    14.                 .Get<AndroidJavaObject>("mUnityPlayer")
    15.                 .Get<AndroidJavaObject>("b");
    16.            
    17.             var decorView = dialog.Call<AndroidJavaObject>("getWindow")
    18.                 .Call<AndroidJavaObject>("getDecorView");
    19.            
    20.             var height = decorView.Call<int>("getHeight");
    21.            
    22.             using (var rect = new AndroidJavaObject("android.graphics.Rect"))
    23.             {
    24.                 view.Call("getWindowVisibleDisplayFrame", rect);
    25.                 return (float) (Screen.height - rect.Call<int>("height") + height) / Screen.height;
    26.             }
    27.         }
    28. #elif UNITY_IOS
    29.         return (float) TouchScreenKeyboard.area.height / Screen.height;
    30. #endif
    31.     }
    I get dialog with EditText (in Unity 2018.2.x this is a field "b") and take the height of this dialog. This code may not work in newer versions of Unity.
     
    Last edited: Mar 20, 2019
    jhina likes this.
  12. Arshia001

    Arshia001

    Joined:
    Sep 20, 2011
    Posts:
    6
    I can confirm this still works as of Unity 2019.1.4. If someday it ceases to work (assuming they don't change the android player considerably), to get the name of the field you need to access instead of "b", decompile Unity's android player jar file, and look inside the showSoftInput method. It'll look like this:

    Code (CSharp):
    1.     protected void showSoftInput(final String string2, final int n2, final boolean bl, final boolean bl2, final boolean bl3, final boolean bl4, final String string3, final int n3, final boolean bl5) {
    2.         this.a(new Runnable(){
    3.  
    4.             @Override
    5.             public final void run() {
    6.                 UnityPlayer.this.b = new k(UnityPlayer.this.r, this, string2, n2, bl, bl2, bl3, string3, n3, bl5);
    7.                 UnityPlayer.this.b.show();
    8.             }
    9.         });
    10.     }
    You want whichever field is instantiated and accessed here.

    Unity devs: can't you at least exclude that field from obfuscation? XD
     
  13. EdyH

    EdyH

    Joined:
    Jun 20, 2019
    Posts:
    4
    Hi
    In Unity 2019.1.5 I have a null reference in this line
    Code (CSharp):
    1. var decorView = dialog.Call<AndroidJavaObject>("getWindow")
    2.                 .Call<AndroidJavaObject>("getDecorView");
    Edit: dialog is null
     
    Last edited: Jun 22, 2019
  14. XUEMINGNI

    XUEMINGNI

    Joined:
    Mar 28, 2019
    Posts:
    1
    I have the same problem. Has anyone solved it
     
  15. pepeims

    pepeims

    Joined:
    Mar 28, 2016
    Posts:
    1
    It works fine on Unity 2019.1.7 and Android 9 phone