Get the height of virtual keyboard in Android

2019-01-09 04:01发布

问题:

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.

回答1:

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.



回答2:

you can use this sample code. it is 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();


回答3:

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.
  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
    
    }
    

    });



回答4:

If you don't want android:windowSoftInputMode="adjustResize" in your app. 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));
        }
    });


回答5:

Try this

KeyboardView keyboardView = new KeyboardView(getApplicationContext(), null);
int height = (keyboardView.getKeyboard()).getHeight();
Toast.makeText(getApplicationContext(), height+"", Toast.LENGTH_LONG).show();