Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 972
Rudra Raina Avatar asked Feb 06 '21 09:02

Rudra Raina


People also ask

Which command is used to hide and display status bar?

View decorView = getWindow(). getDecorView(); // Hide the status bar.

How do I hide the status bar on my Samsung?

Go to Settings > Display >Notification shade > Turn the toggle off for 'Show icons for incoming notifications. '


Video Answer


2 Answers

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()
);
like image 94
hata Avatar answered Sep 22 '22 00:09

hata


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.

like image 32
SlothCoding Avatar answered Sep 24 '22 00:09

SlothCoding