How do I hide the status bar in my Android 11 app?
Asked Answered
R

5

8

Every method I came across to hide status bar of my Android app is deprecated in Android 11.

Does anyone know about any current acceptable method?

Also I use Kotlin to develop my apps.

Replay answered 6/2, 2021 at 9:35 Comment(0)
R
24

When your device is API-30 (Android 11; minSdkVersion 30) or later , setSystemUiVisibility is deprecated and you can use the newly introduced WindowInsetsController instead. (And note that you cannot use WindowInsetsController on API-29 or earlier).

So the official reference says:

This method was deprecated in API level 30. SystemUiVisibility flags are deprecated. Use WindowInsetsController instead.

You should use WindowInsetsController class.

in Kotlin:

window.decorView.windowInsetsController!!.hide(
    android.view.WindowInsets.Type.statusBars()
)

in Java:

getWindow().getDecorView().getWindowInsetsController().hide(
    android.view.WindowInsets.Type.statusBars()
);

If you also want to hide NavigationBar:

in Kotlin:

window.decorView.windowInsetsController!!.hide(
    android.view.WindowInsets.Type.statusBars()
            or android.view.WindowInsets.Type.navigationBars()
)

in Java:

getWindow().getDecorView().getWindowInsetsController().hide(
    android.view.WindowInsets.Type.statusBars()
            | android.view.WindowInsets.Type.navigationBars()
);
Rita answered 6/2, 2021 at 12:35 Comment(2)
I tried to hide the status bar and this error showed up (My API is 21) -> java.lang.NoSuchMethodError: No virtual method getWindowInsetsController()Landroid/view/WindowInsetsController; in class Landroid/view/View; or its super classes (declaration of 'android.view.View' appears in /system/framework/framework.jar!classes3.dex) at com.example.helloworld.MainActivity.onCreate(MainActivity.kt:11)Replay
@RudraRaina If your app is API-21, setSystemUiVisibility is not deprecated and you can use it only. While, only when you use minSdkVersion 30 or later, setSystemUiVisibility is deprecated and you can use windowInsetsController.Rita
H
9

API LEVEL < 16

If you want to hide the status bar in your application you can simply do this by making your app FULLSCREEN. Inside your onCreate method just add FLAG_FULLSCREEN

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_loading);

This is if Build.VERSION.SDK_INT < 16.

API LEVEL >= 16 AND < 30

This is for Build.VERSION.SDK_INT greater than 16;

    View decorView = getWindow().getDecorView();
    // Hide the status bar.
    int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
    decorView.setSystemUiVisibility(uiOptions);

Just add this inside your onCreate where you want to hide the status bar. More you can read here: https://developer.android.com/training/system-ui/status#41


EDIT: API LEVEL >= 30

It seems that SYSTEM_UI_FLAG_FULLSCREEN is also depricated from Android 11 even if it didn't say anything in the documentation. But based on this tutorial you can do this in Android 11 you need to use WindowInsetsController and its hide() method. Like the other answer suggested you can use:

getWindow().getDecorView().getWindowInsetsController().hide(
        android.view.WindowInsets.Type.statusBars()
    );

So, this is for Android 11 and later, other methods are for earlier versions.

Hambletonian answered 6/2, 2021 at 10:31 Comment(4)
SYSTEM_UI_FLAG_FULLSCREEN & SystemUiVisibility are deprecated sir . Although it works but is there any code which is not deprecated ?Replay
Oh, sorry. I didn't see that because it didn't say anything in the documentation I read, you can check the link above. My bad.Hambletonian
Maybe this can help: medium.com/swlh/…Hambletonian
@Hambletonian List of deprecated stuffsCulicid
S
2

This is the easiest way to hide/show status bar in kotlin.

private fun Activity.hideStatusBars() {
    WindowInsetsControllerCompat(window, window.decorView)
        .hide(WindowInsetsCompat.Type.statusBars())
}

private fun Activity.showStatusBars() {
    WindowInsetsControllerCompat(window, window.decorView)
        .hide(WindowInsetsCompat.Type.statusBars())
}

If you need to show/hide both status & navigation bar use systemBars() instead of statusBars() as following example.

private fun Activity.hideSystemBars() {
    WindowInsetsControllerCompat(window, window.decorView)
        .hide(WindowInsetsCompat.Type.systemBars())
}

private fun Activity.showSystemBars() {
    WindowInsetsControllerCompat(window, window.decorView)
        .hide(WindowInsetsCompat.Type.systemBars())
}
Slightly answered 9/5, 2022 at 14:50 Comment(0)
L
1

Keep it simple. If SDK is equal to or greater than 30,

private void hideSystemBars() {
    View decorView = getWindow().getDecorView();
    WindowInsetsController windowInsetsController = decorView.getWindowInsetsController();
    if (windowInsetsController == null) {
        return;
    }
    // Configure the behavior of the hidden system bars
    windowInsetsController.setSystemBarsBehavior(
            WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
    );
    // Hide both the status bar and the navigation bar
    windowInsetsController.hide(android.view.WindowInsets.Type.systemBars());
}
Leonidaleonidas answered 24/6, 2022 at 11:10 Comment(0)
C
-1

Are you looking for this?

// Enables regular immersive mode.
// For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
// Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
window.decorView.systemUiVisibility = (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.
        or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        // Hide the nav bar and status bar
        or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        or View.SYSTEM_UI_FLAG_FULLSCREEN)

Enable fullscreen mode Hide the status bar

Culicid answered 7/2, 2021 at 7:3 Comment(1)
These is the deprecated method asked to avoid.Ianthe

© 2022 - 2024 — McMap. All rights reserved.