Last active
September 22, 2019 12:41
-
-
Save dphans/f61750611a2f7e225e4ef92fa21faebd to your computer and use it in GitHub Desktop.
Detecting deivces night mode programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// check device state is in dark mode | |
val isDarkMode = [email protected] | |
.and(Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES | |
// retrieve current themes background color | |
val typedValue = TypedValue() | |
[email protected](android.R.attr.colorBackground, typedValue, true) | |
// allow activity can update system system bars | |
[email protected](WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) | |
// apply system StatusBar/NavigationBar color based on background color | |
[email protected] = typedValue.data | |
[email protected] = typedValue.data | |
// for Day mode, make light system bars to make dark content visible on light background color. | |
if (!isDarkMode) { | |
[email protected] = | |
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment