The DayNight
Solution
Google’s preferred solution is for you to use a theme that adapts based upon whether the device is in dark mode or not. That way, you can have a light theme “normally” while having a dark theme in dark mode.
In particular, AppCompat supports this via its DayNight
theme family. The basic recipe is:
- Change your theme’s parent theme to
Theme.AppCompat.DayNight
(or to some other theme that extendsTheme.AppCompat.DayNight
). - Define alternative colors, icons, and other resources in
-night
resource sets. For example, your regular (“day”) colors might be inres/values/colors.xml
, while the “night” colors might be inres/values-night/colors.xml
. If you use the same name for the individual resources (e.g.,primary
), Android will choose the proper value to use depending on whether dark mode is enabled.
You can learn more about the
DayNight
option in the "Dark Mode" chapter of Elements of Android Q!
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.