prom85
prom85

Reputation: 17858

Immersive mode - only hide status bar + rehide status bar after some time without user interaction

I have an activity with 2 toolbars, one at the top and one at bottom. I use inversive mode and this works fine now, it allows the user to drag down the status bar...

The problem, the status bar is never hidden again, after it was dragged down...

I found a few solutions here, but I just could not get it working. Either the status bar is shown over my view, or it messes up if I open the overflow menu or it just does not get hidden again...

This is what I currently use:

@Override
protected void onResume()
{
    super.onResume();
    enableFullScreenMode();
}

private void enableFullScreenMode()
{
    if (Build.VERSION.SDK_INT < 16)
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    else
        getWindow().getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_FULLSCREEN
//                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        );
}

What I want

Upvotes: 0

Views: 3989

Answers (3)

bu2zhouzhu
bu2zhouzhu

Reputation: 21

The answer is, Sticker immersive mode.

As the doc says:

"The bars automatically disappear after a few seconds of no interaction or as soon as the user touches or gestures anywhere outside the system bars."

private void hideSystemUI() {
    View decorView = getWindow().getDecorView();
    decorView.setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | 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
                    | View.SYSTEM_UI_FLAG_FULLSCREEN);
}

Upvotes: 2

Shobhit Gupta
Shobhit Gupta

Reputation: 11

//1.put this after on click.

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (hasFocus) {
        hideSystemUI();
    }
}

private void hideSystemUI() {
    // Enables regular immersive mode.
    // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
    // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
    View decorView = getWindow().getDecorView();
    decorView.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
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}

//2.Remove "android:fitsSystemWindows="true"" from everywhere from you layout.

//3.Add hideSystemUI() in onResumes like this ,(for, auto re hiding status and bottom navigation ).

@Override
public void onResume() {
    super.onResume();
    hideSystemUI();
}

Upvotes: 1

aheliver
aheliver

Reputation: 1

try this:

View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
        (new View.OnSystemUiVisibilityChangeListener() {
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        // Note that system bars will only be "visible" if none of the
        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
            // TODO: The system bars are visible. Make any desired
            // adjustments to your UI, such as showing the action bar or
            // other navigational controls.
        } else {
            // TODO: The system bars are NOT visible. Make any desired
            // adjustments to your UI, such as hiding the action bar or
            // other navigational controls.
        }
    }
});

Upvotes: 0

Related Questions