24

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?

aligur
  • 3,387
  • 3
  • 34
  • 51

9 Answers9

42

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)
Community
  • 1
  • 1
Elias Fazel
  • 2,093
  • 16
  • 20
22

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);
}
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • The only way that always works in both directions. But only for API<30. Unfortunately, couldn't find the way to do the same with `WindowInsetsController` on API 30+. – qwertyfinger Mar 01 '21 at 13:55
13

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)
Tong Jing Yen
  • 199
  • 2
  • 12
  • 3
    `yourView` can just be `WindowInsetsControllerCompat(window, window.decorView)` – David Miguel Jun 15 '21 at 09:41
  • @AliZarei can you explain how it didn't work? It seems to work fine for me even with very old versions of the androidx.core library. – Tong Jing Yen Jun 24 '21 at 04:08
  • @TongJingYen status bar's icons is still black after set isAppearanceLightStatusBars to false – Ali Zarei Jun 24 '21 at 05:07
  • 2
    If you set `android:windowLightStatusBar` in the styles, this won't work. You need to call the deprecated function to disable the system UI flag. – Hsingchien Cheng Aug 26 '21 at 08:26
  • for java: new WindowInsetsControllerCompat(window, window.getDecorView()).setAppearanceLightStatusBars(true); – Zack Jan 12 '23 at 14:04
  • Got this working like this: `WindowInsetsControllerCompat(requireActivity().window, view).isAppearanceLightStatusBars = false`, was calling this from onViewCreated() in fragment. Works great on api 26...33 – SonOfaSleep Mar 31 '23 at 12:50
7

To clear this attribute, use this code:

window.clearFlags( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
Chinese Cat
  • 5,359
  • 2
  • 16
  • 17
7
implementation "androidx.core:core-ktx:1.6.0"

Activity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        WindowInsetsControllerCompat(window, window.decorView).isAppearanceLightStatusBars = true
    }
}

Tested on Android 8 and Android 12. Work well

Linh
  • 57,942
  • 23
  • 262
  • 279
4
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
getWindow().setStatusBarColor(Color.WHITE);
Mohammad Wahid
  • 105
  • 3
  • 13
3

Since View.setSystemUiVisibility() is deprecated from API 30 in favor of the new WindowInsetsController API, the 2021 answer to this one is now WindowInsetsControllerCompat#setAppearanceLightStatusBars(boolean), which is backward compatible to API 23. Required androidx.core:core:1.5.0-alpha05 or later.

WindowInsetsControllerCompat.setAppearanceLightStatusBars(true)

hidro
  • 12,333
  • 6
  • 53
  • 53
1

Only do this
it make icon color white

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
Rahul Singh
  • 168
  • 2
  • 6
0

In Activity

If you want change the windowLightStatusBar inside the activity

For Kotlin

val windowInsetController = WindowCompat.getInsetsController(window,window.decorView)
    windowInsetController.isAppearanceLightStatusBars = true

For Java

WindowInsetsControllerCompat windowInsetController = WindowCompat.getInsetsController(getWindow(), getWindow().getDecorView());
        windowInsetController.setAppearanceLightStatusBars(true);

In Fragment

And if you want change the windowLightStatusBar inside the fragment

For Kotlin

  val windowInsetController = WindowCompat.getInsetsController(requireActivity().window,requireActivit().window.decorView)
        windowInsetController.isAppearanceLightStatusBars = true

For Java

WindowInsetsControllerCompat windowInsetController = WindowCompat.getInsetsController(requireActivity().getWindow(), requireActivity().getWindow().getDecorView());
        windowInsetController.setAppearanceLightStatusBars(true);