Get the height of virtual keyboard in Android

前端 未结 6 1439
有刺的猬
有刺的猬 2020-12-01 14:31

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

6条回答
  •  日久生厌
    2020-12-01 15:19

    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));
            }
        });
    

提交回复
热议问题