Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 866
Rudy_TM Avatar asked Nov 23 '12 18:11

Rudy_TM


People also ask

How do I open a soft keyboard?

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. Now restart the emulator.

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.

How do I change the keyboard height on Android?

RELATED: How to Change the Keyboard on Your Android Phone From there, press the gear icon to open the app's Settings. Next, go to “Preferences.” In the “Layout” section, select “Keyboard Height.” There are a bunch of different heights to choose from.


2 Answers

We did it with this

myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {                  @Override                 public void onGlobalLayout() {                      Rect r = new Rect();                     parent.getWindowVisibleDisplayFrame(r);                      int screenHeight = parent.getRootView().getHeight();                     int heightDifference = screenHeight - (r.bottom - r.top);                     Log.d("Keyboard Size", "Size: " + heightDifference);                  }             }); 

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

like image 129
Rudy_TM Avatar answered Sep 20 '22 15:09

Rudy_TM


Rect r = new Rect(); View rootview = this.getWindow().getDecorView(); // this = activity rootview.getWindowVisibleDisplayFrame(r); 

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)

Found id up here: https://github.com/freshplanet/ANE-KeyboardSize/blob/master/android/src/com/freshplanet/ane/KeyboardSize/getKeyboardY.java

like image 26
FDIM Avatar answered Sep 19 '22 15:09

FDIM