19

I would like to implement a button to enable/disable the immersive full screen mode. I'm using those methods but the showSystemUI only shows quickly and hide again...

How to completely exit from immersive mode?

My methods:

// This snippet hides the system bars.
    @SuppressLint("NewApi")
    private void hideSystemUI() {
        try{
            // 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);
        }catch(Exception e){
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    // 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.
    @SuppressLint("NewApi")
    private void showSystemUI() {
        try{
            mDecorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }catch(Exception e){
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

            mDecorView.setVisibility(View.GONE);
            mDecorView.setVisibility(View.VISIBLE);
            WindowManager.LayoutParams attrs = getWindow().getAttributes();
            attrs.flags |= WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
            getWindow().setAttributes(attrs);
            mDecorView.setPadding(0, getStatusBarHeight(), 0, 0);
        }
    }

How to make the content appear under the system bars again?

Felipe Porge Xavier
  • 2,236
  • 6
  • 19
  • 27
  • where you call those methods ? – mohammed momn Feb 10 '14 at 00:20
  • In the onClick method of a ToggleButton. If the FullScreenButton isChecked, calls hideSystemUi. If the FullScreenButton is not checked, call the showSystemUI. (Remember, this is only called on click in fullscreenbutton. – Felipe Porge Xavier Feb 10 '14 at 00:23
  • :) share full code so i can test it – mohammed momn Feb 11 '14 at 20:31
  • The code is only this more two Buttons calling this methods. If you click in Full Screen Mode, you go to immersive full screen, calling the method hideSystemUI. But if you click in Return Full Screen, the method showSystemUI is called but the app don't back to no-full-screen mode... – Felipe Porge Xavier Feb 12 '14 at 18:59

2 Answers2

35

Calling setSystemUiVisibility() with View.SYSTEM_UI_FLAG_VISIBLE clears all flags:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
Philipp E.
  • 3,296
  • 3
  • 34
  • 52
3

What worked for me is the following:

View decorView = activity.getWindow().getDecorView();
decorView.setSystemUiVisibility(0);

I might be wrong but it seems like calling setSystemUiVisibility with 0 clears the flags which were previously applied.

guy.gc
  • 3,359
  • 2
  • 24
  • 39
  • 1
    You are right! See more about it in documentation here: https://developer.android.com/training/system-ui/dim#reveal – Lincoln Aug 06 '19 at 05:20