Get the height of the virtual keyboard in Android

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

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

+15
android android-softkeyboard
May 15 '11 at 15:55
source share
6 answers

You cannot get the height of the keyboard, but you can get the height of your view, what you really want - and you will get this data, which will be sent to the onLayout call in the current view.

+4
Jun 16 '11 at 16:59
source share

you can use this sample code. it's a dirty solution but it works

Thread t = new Thread(){ public void run() { int y = mainScreenView.getHeight()-2; int x = 10; int counter = 0; int height = y; while (true){ final MotionEvent m = MotionEvent.obtain( SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, x, y, INTERNAL_POINTER_META_STATE); final MotionEvent m1 = MotionEvent.obtain( SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, INTERNAL_POINTER_META_STATE); boolean pointer_on_softkeyboard = false; try { getSingletonInstrumentation().sendPointerSync(m); getSingletonInstrumentation().sendPointerSync(m1); } catch (SecurityException e) { pointer_on_softkeyboard = true; } if (!pointer_on_softkeyboard){ if (y == height){ if (counter++ < 100){ Thread.yield(); continue; } } else if (y > 0){ softkeyboard_height = mainScreenView.getHeight() - y; } break; } y--; } if (softkeyboard_height > 0 ){ // it is calculated and saved in softkeyboard_height } else { calculated_keyboard_height = false; } } }; t.start(); 
+1
Mar 12 2018-12-12T00:
source share

This solution is also hacks, but solves the problem (at least for me).

  • I have a temporary view with a transparent background at the bottom of the screen. Thus, this view will be invisible.
  • I added the android:windowSoftInputMode="adjustResize" to the activity tag in the manifest.
  • Now the main story is in onGlobalLayout() . There I calculate the difference between the y axis of the temp view and the height of the 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 } 

    });

+1
May 6 '14 at 7:01
source share

If you do not want android:windowSoftInputMode="adjustResize" in your application. You can try something like this:

  any_view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int height = main_layout.getHeight(); Log.w("Foo", String.format("layout height: %d", height)); Rect r = new Rect(); main_layout.getWindowVisibleDisplayFrame(r); int visible = r.bottom - r.top; Log.w("Foo", String.format("visible height: %d", visible)); Log.w("Foo", String.format("keyboard height: %d", height - visible)); } }); 
+1
Jul 16 '15 at 15:22
source share

try it

 KeyboardView keyboardView = new KeyboardView(getApplicationContext(), null); int height = (keyboardView.getKeyboard()).getHeight(); Toast.makeText(getApplicationContext(), height+"", Toast.LENGTH_LONG).show(); 
0
Sep 08 '14 at 16:33
source share

Suppose the rootView of your layout is RelativeLayout. What you can do is create a CustomRelativeLayout class that extends RelativeLayout and overrides the onSizeChanged method inside it. Therefore, when the soft keyboard opens, the height of the RelativeLayout will change, and this will be reported in the onSizeChanged method.

 public class CustomRelativeLayout extends RelativeLayout { @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); int softKeyboardHeight = oldh - h; // Assuming softKeyboard Opened up if(softKeyboardHeight > oldh * 0.15) { Log.i("Here", Integer.toString(softKeyboardHeight)); // Keyboard has popped up } else { // Not the keyboard } } 

In the manifest file, make these changes so that the action opens in resize mode rather than in pan and scan mode. In resize mode, the layout will be able to resize when the keyboard is opened. To learn more about pan scanning and resizing, visit https://developer.android.com/training/keyboard-input/visibility

 <activity android:name=".MainActivity" android:windowSoftInputMode="adjustResize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 
0
Apr 24 '19 at 19:10
source share



All Articles