androidwebviewandroid-immersive

Detect Hide Keyboard Event on Android


So I need to keep the immersive mode of an app. The app has a fullscreen webview on immersive mode but the problem is the webview content has a textbox. When the user touch on the textbox, it will trigger the softkeyboard which will disable the immersive mode. I solved the problem when textbox lost its focus, it will trigger a javascriptinterface to reactivated immersive mode again. But the problem is the hide/back button when the softkeyboard is shown.

here is the image

I tried onKeyDown, dispatchKeyEvent and onBackPressed but none of them was triggering when debugging.


Solution

  • For getting visibility of soft keyboard, you have to do this :

    contentView.getViewTreeObserver().addOnGlobalLayoutListener(new 
    ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
    
    Rect r = new Rect();
    contentView.getWindowVisibleDisplayFrame(r);
    int screenHeight = contentView.getRootView().getHeight();
    
    int keypadHeight = screenHeight - r.bottom;
    if (keypadHeight > screenHeight * 0.15) {
        // keyboard is opened
    }
    else {
        // keyboard is closed
        }
    }
    });
    

    Happy coding!!