Search code examples
androidandroid-statusbar

set windowlightstatusbar property programmatically


As you know we can set the windowLightStatusBar from xml by following codes.

<item name="android:windowLightStatusBar">true</item>

i need to change this attribute true to false or false to true by programmatically. Is there a way to achive it?


Solution

  • set this if you want to change icons colors

    .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_‌​BAR);
    

    and to reset to default set this

    .setSystemUiVisibility(0);
    

    but if you want to change background color of statusBar use this

    getWindow.setStatusBarColor(ContextCompat.getColor(activity,R.color.my_statusbar_color));
    

    [Update for API 26]

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        window.insetsController?.setSystemBarsAppearance(WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS,
                 WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS)
    } else {
        @Suppress("DEPRECATION")
        window.decorView.systemUiVisibility = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
        } else {
            View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
        }
        
    }
    

    and to clear it

    window.insetsController?.setSystemBarsAppearance(0, APPEARANCE_LIGHT_STATUS_BARS)