Android 10 adds a system-wide dark theme, which preserves battery power for devices with OLED screens, reduces eye strain, and facilitates use in low-light environments.
These guidelines will show you how to implement a dark theme on Android, even on earlier versions of the platform.
1. Declare dependencies
Add the following dependencies to your project:
2. Inherit from a
The easiest way to support a dark theme is to inherit from a
DayNight theme such as
DayNight theme is composed of a
Light theme in the
values directory and a
Dark theme in the
For example, declare a
And then, declare your
3. Use theme attributes for colors
When writing your layouts, use theme attributes or night-qualified resources instead of hard-coded colors to ensure that they display suitable colors in a
Light theme and in a
For example, when you use a
FloatingActionButton, the default
?attr/colorAccent so the
tint should be
?android:attr/textColorPrimaryInverse to ensure that the contrast ratio between the icon and its background is eligible:
Light theme, it will display a
#ffffffff icon on a
Dark theme, it will display a
#de000000 icon on a
4. Allow users to change the app’s theme
Your app should let the user switch between themes, which map directly to one of the following modes:
System defaulton Android 10 and higher -
Set by Battery Saveron Android 9 or earlier -
AppCompatDelegate.setDefaultNightMode to switch the theme for all components in your app. Please note that it is not saved when the app is killed so you should use
Settings to save the user’s choice.
For example, use the following code in your
Activity to change the night mode:
And then, use the following code in your
Application to restore the night mode:
5. Run your app
That’s it, you are ready to run your app and enjoy a dark theme!
For a complete example, check out my sample on GitHub.