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?
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?
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)
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);
}
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)
To clear this attribute, use this code:
window.clearFlags( View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)
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
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
getWindow().setStatusBarColor(Color.WHITE);
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)
Only do this
it make icon color white
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
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);
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);