Search code examples
androidkotlinandroid-11android-statusbarandroid-api-levels

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.


Solution

  • 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()
    );