Dark Theme (Android)
Dark Theme (Android)
Dark theme is available in Android 10 (API level 29) and higher. It has many
benefits:
Dark theme applies to both the Android system UI and apps running on the device.
There are three ways to enable Dark theme in Android 10 (API level 29) and
higher:
• Use the system setting (Settings -> Display -> Theme) to enable Dark theme.
• Use the Quick Settings tile to switch themes from the notification tray (once
enabled).
• On Pixel devices, selecting the Battery Saver mode enables Dark theme at the
same time. Other OEMs may or may not support this behavior.
This ties the app's main theme to the system-controlled night mode flags and gives
the app a default Dark theme (when it is enabled).
Here are the two most important theme attributes to know about:
• Light
• Dark
• Set by Battery Saver (the recommended default option)
When running on Android 10 (API level 29) and higher, the recommended options
are different, to allow the user to override the system default:
• Light
• Dark
• System default (the recommended default option)
Note that if the user selects Light, then Battery Saver will not change that setting.
• Light - MODE_NIGHT_NO
• Dark - MODE_NIGHT_YES
• Set by Battery Saver - MODE_NIGHT_AUTO_BATTERY
• System default - MODE_NIGHT_FOLLOW_SYSTEM
Force Dark analyzes each view of your light-themed app, and applies a dark theme
automatically before it is drawn to the screen. Some developers use a mix of Force
Dark and native implementation to cut down on the amount of time needed to
implement Dark theme.
If your app uses a dark theme (such as Theme.Material), Force Dark will not be
applied. Similarly, if your app's theme inherits from a DayNight theme, Force Dark
will not be applied, due to the automatic theme switching.
Best Practices
Notifications
For launcher widgets, or if your app uses custom notification content views, it is
important to make sure you test the content on both the Light and Dark themes.
In all of these cases, use appropriate theme attributes instead of hardcoded colors.
Launch screens
If your app has a custom launch screen, it may need to be modified so that it
reflects the selected theme.
Remove any hardcoded colors, for example any background colors pointing may
be white. Use the ?android:attr/colorBackground theme attribute instead.
Configuration changes
When the app’s theme changes (either through the system setting or AppCompat)
it triggers a uiMode configuration change. This means that Activities will be
automatically recreated.
In some cases you might want an app to handle the configuration change. For
example, you might want to delay a configuration change because a video is
playing.
An app can handle the implementation of Dark theme itself by declaring that each
Activity can handle the uiMode configuration change:
<activity
android:name=".MyActivity"
android:configChanges="uiMode" />
When an Activity declares it handles configuration changes,
its onConfigurationChanged() method will be called when there is a theme
change.
To check what the current theme is, apps can run code like this:
KOTLINJAVA
val currentNightMode = configuration.uiMode and
Configuration.UI_MODE_NIGHT_MASK
when (currentNightMode) {
Configuration.UI_MODE_NIGHT_NO -> {} // Night mode is not active,
we're using the light theme
Configuration.UI_MODE_NIGHT_YES -> {} // Night mode is active,
we're using dark theme
}