27

I'm trying to implement a fullscreen mode, but for Android 4.4 and up, it shows a blank space there:

BEFORE immersive mode(fullscreen)

enter image description here

and AFTER the toggleFullScreen(false);

enter image description here

as you can see, its doesn't remove it. Here's the code that I'm using to toggle it:

public void toggleFullscreen(boolean fs) {
        if (Build.VERSION.SDK_INT >= 11) {
            // The UI options currently enabled are represented by a bitfield.
            // getSystemUiVisibility() gives us that bitfield.
            int uiOptions = this.getWindow().getDecorView().getSystemUiVisibility();
            int newUiOptions = uiOptions;
            boolean isImmersiveModeEnabled =
                    ((uiOptions | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY) == uiOptions);
            if (isImmersiveModeEnabled) {
                Log.i(getPackageName(), "Turning immersive mode mode off. ");
            } else {
                Log.i(getPackageName(), "Turning immersive mode mode on.");
            }

            // Navigation bar hiding:  Backwards compatible to ICS.
            if (Build.VERSION.SDK_INT >= 14) {
                newUiOptions ^= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
            }

            // Status bar hiding: Backwards compatible to Jellybean
            if (Build.VERSION.SDK_INT >= 16) {
                newUiOptions ^= View.SYSTEM_UI_FLAG_FULLSCREEN;
            }

            // Immersive mode: Backward compatible to KitKat.
            // Note that this flag doesn't do anything by itself, it only augments the behavior
            // of HIDE_NAVIGATION and FLAG_FULLSCREEN.  For the purposes of this sample
            // all three flags are being toggled together.
            // Note that there are two immersive mode UI flags, one of which is referred to as "sticky".
            // Sticky immersive mode differs in that it makes the navigation and status bars
            // semi-transparent, and the UI flag does not get cleared when the user interacts with
            // the screen.
            if (Build.VERSION.SDK_INT >= 18) {
                newUiOptions ^= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            }
            getWindow().getDecorView().setSystemUiVisibility(newUiOptions);
        } else {
            // for android pre 11
            WindowManager.LayoutParams attrs = getWindow().getAttributes();
            if (fs) {
                attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
            } else {
                attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
            }
            this.getWindow().setAttributes(attrs);
        }

        try {
            // hide actionbar
            if
                    (this instanceof AppCompatActivity) {
                if (fs) getSupportActionBar().hide();
                else getSupportActionBar().show();
            } else if
                    (Build.VERSION.SDK_INT >= 11) {
                if (fs) getActionBar().hide();
                else getActionBar().show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
AL.
  • 36,815
  • 10
  • 142
  • 281
user2582318
  • 1,607
  • 5
  • 29
  • 47
  • did you find a solution for this. I am facing the same problem – glo Jun 01 '16 at 08:04
  • @glo the solution given below is working for me. check [this](https://gitlab.com/ankit_aggarwal/ToggleFullScreenDemo.git) code – Ankit Aggarwal Jun 01 '16 at 10:46
  • @glo is this happening in android L and above only or for lower versions also? If so, is it happening with only status or with bottom navigation bar also? – Ankit Aggarwal Jun 03 '16 at 04:36

5 Answers5

47

Please check that you don't have android:fitsSystemWindows="true" in your layout.

At least it solved my case - I had fitsSystemWindows on FrameLayout.

Boris Treukhov
  • 17,493
  • 9
  • 70
  • 91
  • 2
    But i didn't use it but still I am getting this one of error and really I am not understand why I am getting this one of issue. – Peter Mar 20 '18 at 06:39
  • Omg! I thought this line is to solve this problem. It also seems it was added to Drawer layout by default. Removing it solved the problem. Thanks – Mike Keskinov Jun 29 '20 at 20:38
6

I'm new here so I can't comment, but wanted to add something that got me so frustrated me about the above solution. I kept checking my activities and its fragments for android:fitsSystemWindows="true" and it was definitely not there, yet I kept having a gap at the bottom! I was going nuts! It couldn't be this hard to fix this simple thing!

Turns out it also showed up in the Navigation Drawer I added...so be sure to check all your XML!

Jacky Tang
  • 81
  • 1
  • 3
1

Try this:

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    var viewParent = view
    while (viewParent is View) {
        viewParent.fitsSystemWindows = false
        viewParent.setOnApplyWindowInsetsListener { _, insets -> insets }
        viewParent = viewParent.parent as View?
    }
}

What does this do? DialogFragment#onActivityCreated() calls Dialog#setContentView(), which wraps the Dialog's view in a private 'wrapInBottomSheet'. In order to set the proper flags of those wrapper views, we want to set the flags after they are wrapped, e.g. after super.onActivityCreated()

Also watch this talk for info on fitsSystemWindows and window insets.

cliveleehere
  • 316
  • 1
  • 6
1

Just change the android:fitsSystemWindows="true" to android:fitsSystemWindows="false" in your layout file.

Pratik Sharma
  • 165
  • 2
  • 9
0

You need to add this flag to your view View.SYSTEM_UI_FLAG_LAYOUT_STABLE. Try it like this

// 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);
}
android_Muncher
  • 1,057
  • 7
  • 14
  • hi @android_Muncher, it did not solved, =(, im using Fragment but calling the togglefullscreen from the Activity, still show a gray space where the soft buttons was and a black space where notification bar was – user2582318 Dec 29 '15 at 14:31
  • mDecorView from the example I shared should be your view that needs to be full screen. It doesn't matter if it is a fragment or an activity. You can get the current view by using this lien of code. final View decorView = getWindow().getDecorView(); – android_Muncher Dec 29 '15 at 16:39
  • same issue can you please help ? – Abhishek Tomar Oct 11 '18 at 02:15