64

I am trying to create a button where I can hide or show the status bar on my tablet.

I've put in the onCreate

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

and in the buttons show:

WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags &= ~WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);

hide:

WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
getWindow().setAttributes(attrs);

Any hints/tipps?

//edit

I've looked at this hint here: http://android.serverbox.ch/?p=306 and changed my code like this:

private void hideStatusBar() throws IOException, InterruptedException {
    Process proc = Runtime.getRuntime().exec(new String[]{"su","-c","service call activity 79 s16 com.android.systemui"});
    proc.waitFor();
}

private void showStatusBar() throws IOException, InterruptedException {
    Process proc = Runtime.getRuntime().exec(new String[]{"am","startservice","-n","com.android.systemui/.SystemUIService"});
    proc.waitFor();
}

So if I click on my buttons an the methods are called I can see that something is happening because the app is waiting some seconds. I also looked into LockCat and see that something is happening.

show: http://pastebin.com/CidTRSTi hide: http://pastebin.com/iPS6Kgbp

B770
  • 1,272
  • 3
  • 17
  • 34

10 Answers10

158

Do you have the fullscreen theme set in the manifest?

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

I don't think you'll be able to go fullscreen without this.

I would use the following to add and remove the fullscreen flag:

// Hide status bar
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Show status bar
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
Rob
  • 7,039
  • 4
  • 44
  • 75
  • 1
    No, I haven't added it to the manifest. But when I try, I get the error: Error: No resource found that matches the given name (at 'theme' with value '@android:style/ Theme.Dark.NoTitleBar.Fullscreen')." – B770 Nov 25 '11 at 19:19
  • 1
    @B770: There is none named `Theme.Dark.NoTitleBar.Fullscreen`. There is one named `Theme.NoTitleBar.Fullscreen`. However, you cannot get rid of the system bar on Android 3.0+ devices that lack off-screen HOME and BACK buttons. – CommonsWare Nov 25 '11 at 19:31
  • @CommonWare: I found this: http://forum.xda-developers.com/showthread.php?t=1265397 here it's possible to hide and show the status bar. I've installed the "Overcome" ROM where this funktion is included and it works. So I tought it would be also possible to put this funktion on an other button... – B770 Nov 25 '11 at 19:39
  • ok. I've got it. I forgot to grant su rights to the app. Now everything just works fine :) – B770 Dec 06 '11 at 09:07
  • Is it possible to show/hide status bar when scrolling up or down? – Zahra.HY Aug 21 '16 at 06:15
  • @Rob this is working fine one issue i'm facing is when i do add and clear it shows white statusbar for few milliseconds. which is looking very ood. Any solution for this? – Nouman Ch Apr 27 '19 at 09:35
38

For Some People, Showing status bar by clearing FLAG_FULLSCREEN may not work,

Here is the solution that worked for me, (Documentation) (Flag Reference)

Hide Status Bar

// Hide Status Bar
if (Build.VERSION.SDK_INT < 16) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else {
   View decorView = getWindow().getDecorView();
  // Hide Status Bar.
   int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
   decorView.setSystemUiVisibility(uiOptions);
}

Show Status Bar

   if (Build.VERSION.SDK_INT < 16) {
              getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    else {
       View decorView = getWindow().getDecorView();
      // Show Status Bar.
       int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
       decorView.setSystemUiVisibility(uiOptions);
    }
Hakem Zaied
  • 14,061
  • 1
  • 23
  • 25
Pradeep
  • 6,303
  • 9
  • 36
  • 60
5

used for kolin in android for hide status bar in kolin no need to used semicolon(;) at the end of the line

window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

in android using java language for hid status bar

getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
Prabh deep
  • 1,024
  • 11
  • 15
4

I know its a very old question, but just in case anyone lands here looking for the newest supported way to hide status bar on the go programmatically, you can do it as follows:

window.insetsController?.hide(WindowInsets.Type.statusBars())

and to show it again:

window.insetsController?.show(WindowInsets.Type.statusBars())
Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
  • 1
    I think should add info about using this way for minSdk < 30: ViewCompat.getWindowInsetsController(window.decorView)?.hide(WindowInsetsCompat.Type.systemBars()) – Nail Shaykhraziev Jun 07 '22 at 23:37
2

I've tried so many things.

Finally, It is the most suitable code to hide and show full screen mode.

private fun hideSystemUi() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        window.setDecorFitsSystemWindows(true)
    } else {
        // hide status bar
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_IMMERSIVE or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
    }

}

private fun showSystemUi() {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        window.setDecorFitsSystemWindows(false)
    } else {
        // Show status bar
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.decorView.systemUiVisibility = SYSTEM_UI_FLAG_LAYOUT_STABLE
    }

}

It Implemented it in this app : Android Breakdown.

Go to Videos(Bottom Bar) > Play Any Video > Toggle Fullscreen

Mayank Sharma
  • 739
  • 8
  • 13
1
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //hide status bar
    requestWindowFeature( Window.FEATURE_NO_TITLE );
    getWindow().setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN );


    setContentView(R.layout.activity_main);
}
1

For Kotlin users

TO SHOW

activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

TO HIDE

activity?.window?.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)

Muhamed Riyas M
  • 5,055
  • 3
  • 30
  • 31
1

with this method, using SYSTEM_UI_FLAG_IMMERSIVE_STICKY the full screen come back with one tap without any implementation. Just copy past this method below and call it where you want in your activity. More details here

private void hideSystemUI() {
    getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_IMMERSIVE
                    // Set the content to appear under the system bars so that the
                    // content doesn't resize when the system bars hide and show.
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    // Hide the nav bar and status bar
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN);
}
Nicoolasens
  • 2,871
  • 17
  • 22
0

Reference - https://developer.android.com/training/system-ui/immersive.html

// 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);
}
Varun Bhatia
  • 4,326
  • 32
  • 46
0
fun Activity.setStatusBarVisibility(isVisible: Boolean) {
    //see details https://developer.android.com/training/system-ui/immersive
    if (isVisible) {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN)
    } else {
        window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_FULLSCREEN)
    }
}
Yazon2006
  • 3,684
  • 3
  • 25
  • 36