Android get keyboard height

Get the height of virtual keyboard in Android

How can I get the height of virtual keyboard in Android? Is it possible?

I try to get it from the main window, but it gives me full height of the application. But I want to get the keyboard height.

6 Answers 6

You can’t get the keyboard height, but you can get the height of your View, which is what you really want — and you’ll get this data supplied to the onLayout call into the current view.

you can use this sample code. it is dirty solution but it works

This solution is also hacky but solve the problem (atleast for me).

  1. I have places on temporary view with transparent background at the bottom of the screen. So this view will be invisible.
  2. I have added android:windowSoftInputMode=»adjustResize» flag in activity tag in manifest.

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() <

If you don’t want android:windowSoftInputMode=»adjustResize» in your app. You can try something like this:

Let’s suppose that the rootView of your layout is RelativeLayout. What you can do create a class CustomRelativeLayout which extends RelativeLayout and Overrides onSizeChanged Method inside it. So when the soft keyboard opens up, the height of the RelativeLayout will change and the change will be notified inside the onSizeChanged Method.

Источник

How to get Height of keyboard on Android and iOS (Xamarin Forms)?

I want to get the height of the Keyboard on Android and iOS ( Xamarin Forms ).

When Screen display with Portrait mode and Landscape mode, how to get height value?

I have found references with Swift on iOS :

But can provide me with the source code on Xamarin

And how to get the height of the Keyboard on Android ?

2 Answers 2

For iOS

There’s two ways of doing this!

First Way: Implement it for one UIViewController, if your app is small, as follows:

Step 1: Add fields for the show and hide observers:

Step 2: Update the ViewDidLoad and ViewDidUnload overrides to add/remove the observers to whenever the keyboard is displayed or hidden:

Step 3: Then you populate the functions KeyboardDidShowNotification & KeyboardWillHideNotification that execute for each observer. It’s fairly long and would take me a while to explain each part of, but you can ask me in the comments. It goes as follows:

Second Way: Create a keyboard handler that you can use for each page through your BaseViewController

For Android:

You can follow the solutions mentioned in the stackOverflow Xamarin solution here ((Activity)Forms.Context).Window.SetSoftInputMode(SoftInput.AdjustPan); . Some times that doesn’t work, so you can use this to fix it. PS: Didn’t copy these answers because it doesn’t make sense to rewrite an answer.

To get the height of the keyboard programmatically in a Xamarin.Forms project, you will need to register a platform service.

The interface

Please note: I have used a Subject (contained in the System.Reactive.Subjects namespace) to notify about the keyboard height change but you could also use an event, if this concept fits better in your project. If you stay with the Subject, you may need to install the System.Reactive NuGet package.

iOS implementation

Android implementation

On Android you also need a listener. This is the implementation:

Источник

Getting the dimensions of the soft keyboard

Is there a way to know the size of the keyboard that is shown in the screen?

I am using Cocos2dx for programming, but I want to know the height of the keyboard shown in screen in the part of Android or the part of Cocos, it does not matter.

I know that Keyboard has a getHeight() method but I don’t want to create new keyboards, i want to use the default one.

12 Answers 12

We did it with this

We only resize views with the keyboard, so we could use this.

Result of this is the amount of space your application uses on screen (works even when activity is not resized). Obviously remaining screen space will be used by the keyboard ( if its visible)

if your activity is not fullscreen, using code below:

If you want to calculate the Virtual Keyboard height while your activity does not change in size (adjustPan) then you can use this sample:

It uses a hidden window in order to calculate the height difference between the window and the root view of the activity.

You can’t tell. No, really: you simply can’t tell.

The keyboard does not need to be any particular shape. It does not have to be placed at the bottom of the screen (many of the most popular options are not), it does not have to keep its current size when you change text fields (almost none do depending on the flags). It does not even have to be rectangular. It may also just take over the entire screen.

I know this is an old post, but I noticed that the chosen solution for me did not work on all devices. There seemed to be a discrepancy and so I implemented this and it seems to be a catch all:

When the listener is first called it measures the screen without a keyboard and if there is a discrepancy I account for it the next time around. If there is no discrepancy I set the discrepancy to 1 just so it is no longer 0.

in cocos2d-x we have got CCEditBox.

Inside Extensions->GUI->CCEditBox, you can find the class CCEditBox.

The beauty is that it hides the keyboard of tapping somewhere else on the scene. and automatically moves the keyboard up incase your edit box was placed too low on the scene.

If you are using cocos2d-x v2.1.3 then you can navigate to sample Project by going to

I’m just going to use it instead of CCTextField from now on. just came across it yesterday 🙂

After hours of searching I found a solution if you want to set windowSoftInput=»adjustPan»

Here is the code snippet:

In this code, after I found the keyboard height I scroll the view up to not covered by the keyboard which is the main reason for finding the keyboard height.

According to the docs :

void getWindowVisibleDisplayFrame(Rect outRect) : Retrieve the overall visible display size in which the window this view is attached to has been positioned in.

After 2020, if your min SDK large or equal then 21, you can check the visibility and height of IME by below functions:

The ROOT_VIEW of an android display screen can be visualized as being a single screen view with VISIBLE DISPLAY FRAME which displays your activity’s view.

This VISIBLE DISPLAY FRAME is adjusted when SOFT KEYBOARD is displayed or hidden from the screen.

NOTE : Please look at the two images by clicking on the links given below for better understanding

So the ROOT VIEW of a display screen can be visualized as : RootView of display screen

The adjustment of VISIBLE DISPLAY FRAME with the opening and closing of SOFT KEYBOARD can be visualized as : VISIBLE_DISPLAY_SCREEN adjustment

This adjustment of the VISUAL DISPLAY FRAME can be very well used to find out the height of the keyboard as :

(when the soft keyboard is open)

SOFT_KEYBOARD_HEIGHT = ROOT_VIEW_HEIGHT — (VISUAL_DISPLAY_FRAME_HEIGHT + EXTRA_SCREEN_HEIGHT)

The code to achieve the above is :

NOTE : The onGlobalLayout() function will be called only when the global layout changes like when the soft keyboard opens. So the soft keyboard must be open at least once to get the soft keyboard height.

Источник

Is there any way in Android to get the height of virtual keyboard of device

Is there any way in Android to get the height of the virtual keyboard displayed on the device in run time? Actually I want to show a text box above the keyboard.

13 Answers 13

To solve this I have written a keyboardHeightProvider which can calculate the height of a floating soft keyboard. The Activity can be set to adjustNone or adjustPan in the AndroidManifest.xml.

I tried many suggested methods for this, but none seemed to work for Android SDL. I think this is either because the SDL display is «full screen» or that it sits within an «AbsoluteLayout» and therefore the height of the «View» never actually changes. This method worked for me:

Yes you can, with the help of Viewtree Observer and global layout listener, just try below mentioned steps

  1. Get the root view of your layout
  2. get the Viewtree observer for this root, and add a global layout listener on top of this.

now whenever soft keyboard is displayed android will re-size your screen and you will receive call on your listener. That’s it only thing you now need to do is calculate difference between height which your root view has after re-size and original size. If difference is more then 150 consider this as a keyboard has been inflated.

Below is a sample code

put the text box as parent bottom.

and in the manifest file make the soft input adjustresize

then the text box will move up when the keyboard appears.

You can’t tell. No, really: you simply can’t tell.

The keyboard does not need to be any particular shape. It does not have to be placed at the bottom of the screen (many of the most popular options are not), it does not have to keep its current size when you change text fields (almost none do depending on the flags). It does not even have to be rectangular. It may also just take over the entire screen.

I’ve created a library project get android keyboard height, even when activities don’t use the adjustResize input mode.

This method works with adjustNothing or any windowSoftInputMode on your activity.

Using a PopupWindow , you can have separate «keyboard behaviour» for it, and it will notify you what the size of the keyboard is. The PopupWindow has the height of the screen, but 0px width, so you won’t see it, it won’t affect your activity, but will provide you with the information you need.

Create a class called KeyboardHeightProvider and add the following code:

Notice how the PopupWindow has its own setSoftInputMode(. ) , so it doesn’t matter what you set your activity to, the PopupWindow will still be affected by the keyboard opening or closing and will provide the parent activity of the height. If the height is >= 100 you can assume the keyboard is open.

To use it, simply instantiate it in your Activity’s onCreate(. ) method after setContentView(. ) :

My solution is the combination of all above solutions. This solution is also hacky but solve the problem (atleast for me).

  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.

Now main story is in onGlobalLayout() . There i calculate the difference between the y axis of temp view and height of root view

But anyways to solve the problem of @zeeshan0026 only a single flag in manifest android:windowSoftInputMode=»adjustResize» is enough.

I have used this to get keyboard height programmatically in android and tested it, please try this once:

As the navigation bars, keyboard, etc are added to the window, you can measure these insets to check if the keyboard is open or not. With Android R, you can measure the keyboard directly, but you can fall back to calculating the keyboard size from the insets for prior versions.

This works on Lollipop and up.

Look no more! I’ve been looking for a solution for this for a long time. After days of trying all the tricks suggested on SOF, I found the perfect solution that actually works.

This GitHub project demonstrates it the best way possible: https://github.com/siebeprojects/samples-keyboardheight

I finally found the solution to get the height of soft/virtual keyboard. I cannot say this works on all devices, but I tried on some devices both real and emulator devices and it works. I tried on devices from Android API 16 to 29. It is a little bit tricky. Here is my analysis.

First, I tried to subtract between the height of the screen and height of visible frame on top of soft/virtual keyboard by using function of getHeightDifference() such as below. I found that at the first time the layout is created, right before the keyboard open on EditText’s focus, the difference height will depend on Android System Navigation Bar, either the Navigation Bar is shown inside the device screen or not. So, the value of heightDifference will be 0 if the Nav Bar outside the screen or greater than 0 if it is inside the screen. I’m using a variable of systemNavigationBarHeight with Integer object (instead of using primitive int data) to save the first value of that height difference with only one initialization, which I assume that is the height of Nav Bar.

And then in next code block, I check if next height difference is greater than the height of actual Navigation Bar in Android system (or 100 is default just in case if there is no Navigation Bar in Android system), then I subtract again with the value of systemNavigationBarHeight to get the real height of soft/virtual keyboard.

Hope this will help on someone who look up another answer.

Источник

Читайте также:  Где андроид хранит тебя
Оцените статью