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
RELATED: How to Change the Keyboard on Your Android Phone Next, go to “Preferences.” In the “Layout” section, select “Keyboard Height.”
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.
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!
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.
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
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With