Immersive full-screen toolbar

Can I 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(); } 

It works great. But the toolbar is no longer displayed.

+6
source share
3 answers

try it

  @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); } } } 
+3
source

This may help others, I decided to use View.OnSystemUiVisibilityChangeListener here the snipet code

 -------- 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 a complete code example

+1
source

I discovered a new problem in the official Google immersive project code sample. We hope that they will provide an official way to process the toolbar in dive mode.

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

0
source

Source: https://habr.com/ru/post/983102/


All Articles