javaandroidandroid-studionavigationbarandroid-navigation-bar

How can I make the navigation bar visible only when the soft keyboard is open?


I have an app that should be full screen most of the time. However, I want users to be able to easily go back when using the keyboard. How can I make the navigation bar visible only when the keyboard is open? When the keyboard is turned off, the navigation bar should disappear again.


Solution

  • I added this library to my project. So I was able to type what to do when the keyboard is open:

    implementation 'com.github.ravindu1024:android-keyboardlistener:1.0.0'
    

    Then I created two values, one to show and one to hide:

    private int showSystemBars() {
        return SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN  |
                SYSTEM_UI_FLAG_LAYOUT_STABLE;
    }
    
    private int hideSystemBars() {
        return SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
    }
    

    Then I used the library I added:

    KeyboardUtils.addKeyboardToggleListener(this, isVisible -> {
            if (isVisible) {
                Toast.makeText(context, "Visible", Toast.LENGTH_SHORT).show();
                getWindow().getDecorView().setSystemUiVisibility(showSystemBars());
            }else {
                Toast.makeText(context, "Invisible", Toast.LENGTH_SHORT).show();
                getWindow().getDecorView().setSystemUiVisibility(hideSystemBars());
            }
        });