Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Android soft keyboard height?

I know I can use "android:windowSoftInputMode="adjustPan" to do some automatically offset. But I really want to get software keyboard height for some special purpose.

I got to find that there's a similar topic here: Getting the dimensions of the soft keyboard. But obviously, it's not an universal solution.

Is there a common way or built-in method to get soft keyboard height? (or how can I get the offset value between current cursor position and software keyboard top position?)

Thanks so much

like image 241
liangwang Avatar asked Aug 26 '13 00:08

liangwang


People also ask

How to get height keyboard in Android?

RELATED: How to Change the Keyboard on Your Android Phone Next, go to “Preferences.” In the “Layout” section, select “Keyboard Height.”

How do I make the keyboard softer on my Android?

By default, the soft keyboard may not appear on the emulator. If you want to test with the soft keyboard, be sure to open up the Android Virtual Device Manager ( Tools => Android => AVD Manager ) and uncheck "Enable Keyboard Input" for your emulator.

How do I know what height my keyboard is fluttering?

To know about the keyboard height, you can just check for the bottom property of viewInsets , when the keyboard is onscreen, this will hold the height of keyboard else zero. Note: The bottom property may have value even if some other system ui obscures the flutter ui from bottom. Hope that helps!

What is soft keyboard in Android?

The soft keyboard (also called the onscreen keyboard) is the main input method on Android devices, and almost every Android developer needs to work with this component at some point.


1 Answers

Get Soft Keyboard Height in Xamarin.Android, use ViewTreeObserver.IOnGlobalLayoutListener to listen for GlobalLayout Change event and calculate the change difference in root view before and after in order to get the height of keyboard. You can do similar in Native Android Code.

here is the code:

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
        public static Android.Views.View RootView = null;
        public void DetectSoftKeyboardHeight()
        {
            RootView = this.FindViewById(Android.Resource.Id.Content);
            if(RootView!=null)
                RootView.ViewTreeObserver.AddOnGlobalLayoutListener(new MyLayoutListener());
        }
}
/// <summary>
/// My layout listener.
/// Detect Android Soft keyboard height
/// </summary>
public class MyLayoutListener : Java.Lang.Object, ViewTreeObserver.IOnGlobalLayoutListener
{
    public void OnGlobalLayout()
    {
        // do stuff here
        Android.Graphics.Rect r = new Android.Graphics.Rect();
        if (Mobibranch.Droid.MainActivity.RootView != null)
        {
            Mobibranch.Droid.MainActivity.RootView.GetWindowVisibleDisplayFrame(r);

            int screenHeight = Mobibranch.Droid.MainActivity.RootView.RootView.Height;
            int keyboardHeight = screenHeight - (r.Bottom);

            if (keyboardHeight > 0)
            {
                //Keyboard is up on screen
                Android.Util.Log.Verbose("[[[[MyLayoutListener]]]]", "Keyboard is up on screen, Height: "+keyboardHeight);
            }
            else
            {
                //Keyboard is hidden
            }
        }
    }
}        
like image 124
chris hu Avatar answered Nov 15 '22 11:11

chris hu