Toolbar with Immersive Full-Screen Mode
Asked Answered
W

3

6

Is it possible to use Immersive Mode on Android KitKat and Lollipop in combination with a Toolbar?

Toolbar

 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

hide Action Bar (Toolbar)

    // This snippet hides the system bars.
    // https://developer.android.com/training/system-ui/immersive.html
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.

    getWindow().getDecorView().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);

    getSupportActionBar().hide();
}

This works fine. But the Toolbar doesn't appear again.

Wieland answered 27/2, 2015 at 13:14 Comment(1)
Hi, did you find the solution? Can you please share?Alternator
S
3

Try this

 @Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        if (hasFocus) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
    }
}
Santalaceous answered 17/3, 2016 at 14:31 Comment(0)
A
2

I opened a new issue in the official Google immersive sample project code. Hope they will provide an official way to handle toolbar in immersive mode.

https://github.com/googlesamples/android-BasicImmersiveMode/issues/1

Accustomed answered 28/2, 2017 at 0:0 Comment(0)
A
1

This may help others, I have solved using View.OnSystemUiVisibilityChangeListener here is code snipet

--------

 View mDecorView = getWindow().getDecorView();
 mDecorView.setOnSystemUiVisibilityChangeListener(mOnSystemUiVisibilityChangeListener);
-------


hideSystemUI();

---------


 private View.OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener = new View.OnSystemUiVisibilityChangeListener() {
    @Override
    public void onSystemUiVisibilityChange(int visibility) {
        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == View.VISIBLE) {
            mToolBar.startAnimation(mSlideDown);
            getSupportActionBar().show();
        } else {
            getSupportActionBar().hide();
            mToolBar.startAnimation(mSlideUp);
        }
    }
};

Find complete sample code

Alternator answered 2/5, 2015 at 8:36 Comment(2)
I will give it a try. How do the animations look like?Wieland
The animations are same as the default ActionBar hides(scrolls up) and show(scrolls down) in immersive mode.Alternator

© 2022 - 2024 — McMap. All rights reserved.