Dark mode on Android
Dark mode darkens background colours and provides a more comfortable viewing experience in low light. This is essential for many visually impaired people since a light background colour can be painful or even impossible to look at for an extended period of time. But also people who have problems with their concentration, for example due to a brain disorder, benefit greatly from dark mode.
On Android, you can detect dark mode by checking if the uiMode
configuration contains UI_MODE_NIGHT_MASK
.
By adding -night
resources to your project you can let Android automatically pick the right resources. For example, add a colors.xml
file inside a values-night
folder to specify night mode colors.
fun Context.isInNightMode(): Boolean {
val nightModeFlags = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
return nightModeFlags == Configuration.UI_MODE_NIGHT_YES
}