'Listen to statusbar/navbar visibility in fullscreen mode?

In my activity I go fullscreen to hide the status bar and navigation bar using this code:

window.apply {
    decorView.systemUiVisibility = (
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_IMMERSIVE
            )
    addFlags(
        WindowManager.LayoutParams.FLAG_FULLSCREEN
                or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
    )
}

Android make it so the status bar and navigation bar can still temporary appear with a slide from the bottom or the top of the screen, how can I detect when this event happen? (it seems possible since the default Actionbar automatically show/hide with the status bar)



Solution 1:[1]

After a little bit more search I found a solution for both deprecated API and new API.

You can use this;

private void setSystemUiVisibilityListener() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        //Use this for Devices which has SDK Version greater then R
        yourViewToListen.setOnApplyWindowInsetsListener((v, insets) -> {
            WindowInsets suppliedInsets = v.onApplyWindowInsets(insets);
            //Listen for only status bar and navigation bar
            if (suppliedInsets.isVisible(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars())) {
                //Do whatever you want when system status bar OR navigation bars are visible
            }
            return suppliedInsets;
        });
    } else { //And use this for older SDK versions (It is deprecated on new SDKs)
        yourViewToListen.setOnSystemUiVisibilityChangeListener(visibilityChangeListeners -> {
            if ((visibilityChangeListeners & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                //Do whatever you want when system status bar OR navigation bars are visible
            }
        });
    }
}

Solution 2:[2]

Follow this link : Full-Screen Mode.

// This snippet hides the system bars.
private void hideSystemUI() {
    // Set the IMMERSIVE flag.
    // Set the content to appear under the system bars so that the content
    // doesn't resize when the system bars hide and show.
    mDecorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
            | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
            | View.SYSTEM_UI_FLAG_IMMERSIVE);
}

// This snippet shows the system bars. It does this by removing all the flags
// except for the ones that make the content appear under the system bars.
private void showSystemUI() {
    mDecorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 S_i_l_e_n_t C_o_d_e_r