Skip to content

Instantly share code, notes, and snippets.

@dphans
Last active September 22, 2019 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dphans/f61750611a2f7e225e4ef92fa21faebd to your computer and use it in GitHub Desktop.
Save dphans/f61750611a2f7e225e4ef92fa21faebd to your computer and use it in GitHub Desktop.
Detecting deivces night mode programmatically
// check device state is in dark mode
val isDarkMode = this@BaseActivity.resources.configuration.uiMode
.and(Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES
// retrieve current themes background color
val typedValue = TypedValue()
this@BaseActivity.theme.resolveAttribute(android.R.attr.colorBackground, typedValue, true)
// allow activity can update system system bars
this@BaseActivity.window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
// apply system StatusBar/NavigationBar color based on background color
this@BaseActivity.window.statusBarColor = typedValue.data
this@BaseActivity.window.navigationBarColor = typedValue.data
// for Day mode, make light system bars to make dark content visible on light background color.
if (!isDarkMode) {
this@BaseActivity.window.decorView.systemUiVisibility =
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