2023 - Verwendung von windowInsetController
fun Activity.setSystemBars(
lightStatusBarColor: Int = Color.WHITE,
darkStatusBarColor: Int = Color.BLACK,
lightNavigationBarColor: Int = Color.WHITE,
darkNavigationBarColor: Int = Color.BLACK,
) {
when (resources.configuration.uiMode.and(Configuration.UI_MODE_NIGHT_MASK)) {
Configuration.UI_MODE_NIGHT_YES -> {
setSystemBarAppearance(
statusBarColor = getColorFromId(darkStatusBarColor),
navigationBarColor = getColorFromId(darkNavigationBarColor),
isAppearanceLightNavigationBars = false,
isAppearanceLightStatusBars = false
)
}
Configuration.UI_MODE_NIGHT_NO -> {
setSystemBarAppearance(
statusBarColor = getColorFromId(lightStatusBarColor),
navigationBarColor = getColorFromId(lightNavigationBarColor),
isAppearanceLightNavigationBars = true,
isAppearanceLightStatusBars = true
)
}
Configuration.UI_MODE_NIGHT_UNDEFINED -> {
setSystemBarAppearance()
}
}
}
fun Activity.setSystemBarAppearance(
statusBarColor: Int = Color.WHITE,
navigationBarColor: Int = Color.WHITE,
isAppearanceLightStatusBars: Boolean = true,
isAppearanceLightNavigationBars: Boolean = true
) {
window.statusBarColor = statusBarColor
window.navigationBarColor = navigationBarColor
val windowInsetController = WindowCompat.getInsetsController(window, window.decorView)
windowInsetController.isAppearanceLightStatusBars = isAppearanceLightStatusBars
windowInsetController.isAppearanceLightNavigationBars = isAppearanceLightNavigationBars
}
fun Context.getColorFromId(id: Int) = ContextCompat.getColor(this, id)