Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 221
Ali Gürelli Avatar asked Mar 14 '17 15:03

Ali Gürelli


3 Answers

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)
like image 53
Elias Fazel Avatar answered Nov 03 '22 23:11

Elias Fazel


I believe this is the correct way to turn on and turn off.

if (on) {
    View view = getWindow().getDecorView();
    view.setSystemUiVisibility(view.getSystemUiVisibility() | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
    View view = getWindow().getDecorView();
    view.setSystemUiVisibility(view.getSystemUiVisibility() & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
like image 41
Cheok Yan Cheng Avatar answered Nov 03 '22 23:11

Cheok Yan Cheng


Hidro's answer is almost correct, but WindowInsetsControllerCompat needs to be called as a function to work, otherwise it claims there is an unresolved reference in my case.

For Kotlin:

WindowInsetsControllerCompat(window, yourView).isAppearanceLightStatusBars = true

For Java:

WindowInsetsControllerCompat(getWindow(), yourView).setAppearanceLightStatusBars(true)
like image 9
Tong Jing Yen Avatar answered Nov 03 '22 23:11

Tong Jing Yen