• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by moghes · Jun 21, 2018 at 04:20 PM · keyboardinputfieldnative plugin

Native keyboard height and hide the input field

Hello everyone,

Its just a pain to get the native keyboard height in unity and then hide the native input field. Hiding the input field work fine on ios and never on android.

For Getting the keyboard height I have searched and came out with several solutions which never worked.

For android none of the below worked for me. printing them result in 0.

1-

 TouchScreenKeyboard.area.height

2-

 TouchScreenKeyboard.area.max.y

3 -

 public int GetAndroidKeyboardSize()
         {        
             using(AndroidJavaClass UnityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
             {
                 AndroidJavaObject View = UnityClass.GetStatic<AndroidJavaObject>("currentActivity").Get<AndroidJavaObject>("mUnityPlayer").Call<AndroidJavaObject>("getView");
     
                 using(AndroidJavaObject Rct = new AndroidJavaObject("android.graphics.Rect"))
                 {
                     View.Call("getWindowVisibleDisplayFrame", Rct);
                     return Screen.height - Rct.Call<int>("height");
                 }
             }
         }

4 -

 private float GetKeyboardHeightRatio() 
         {
             #if 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")) / Screen.height;
                 }
             }
             #else
             return (float)TouchScreenKeyboard.area.height / Screen.height;
             #endif
         }

Similarly no success on ios with methods 1&2 since 3&4 are only for android.

Temporarily I used a static size as the keyboard size (40% of canvas size) and the UI scaling works but just can't get the value of the soft native keyboard.

And now want to hide the native input field, this time the trouble is only in android.

TouchScreenKeyboard.hideInput = true; ------ doesn't work this.GetComponent ().shouldHideMobileInput = true; ---- doesn't work

Now before thinking of a plugin just to get a size or hiding the input field, let me know if someone has the simpler solution.

If we need a native plugin for this ... too bad to unity.

Any help or useful links are appreciated. btw I think I've checked all possible links, please suggest a method you've tried and worked for you.

I am using Unity 2017.3.1

thanks in advance

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by shweta-unity · Aug 04, 2020 at 09:19 AM

Hi @moghes, I know this is an old question, but did you get any solution to this? I am also stuck with the issue.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Nikel_MIF · Nov 14, 2020 at 09:08 PM

@moghes

 TouchScreenKeyboard.area

Always return zero for Android - it`s written in the manual.

As far as I know code samples, which call native Android to get keyboard height will be work just when keyboard will be opened fully, you should await several seconds, before call this methods or use coroutines and with help yeild return new WaitUntil(predicate <bool>) wait until result will be non-zero, but I can`t say, that it`s best solution.

About inputfield - can`t remember any solutions at the Unity side, if I find any code snippets related to this question - I`ll write here.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

88 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with Virtual Keyboard 0 Answers

Inputmode on 18:9 mobile device shows navigation bar 0 Answers

Disabling the inputfield above the keyboard or disable it all 0 Answers

Unity InputField is not scrolling in Scollview? 0 Answers

Mobile Keyboard Scales Screen 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges