7

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

Community
  • 1
  • 1
liangwang
  • 707
  • 1
  • 6
  • 15

2 Answers2

2

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
            }
        }
    }
}        
chris hu
  • 458
  • 3
  • 9
0

Here my solution, it is also hacky but solve the problem.

  1. I have places on temporary view with transparent background at the bottom of the screen.
  2. I have added android:windowSoftInputMode="adjustResize" flag in activity tag in manifest like @bill suggests.
  3. Now main story is in onGlobalLayout(). There i calculate the difference between the y axis of temp view and height of root view

    final View view = findViewById(R.id.base);
    view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
    @Override
    public void onGlobalLayout() {
    
        int rootViewHeight = view.getRootView().getHeight();
        View tv = findViewById(R.id.temp_view);
        int location[] = new int[2];
        tv.getLocationOnScreen(location);
        int height = (int) (location[1] + tv.getMeasuredHeight());
        deff = rootViewHeight - height;
        // deff is the height of soft keyboard
    }
    });
    
Renjith Krishnan
  • 2,446
  • 5
  • 29
  • 53
Gem
  • 1,516
  • 16
  • 21