Challenge 3: Dark Mode Colors — Solution
// res/values/colors.xml — light mode (default)
#FFFFFF
#1A1A1A
// res/values-night/colors.xml — dark mode (same names, inverted values)
#121212
#E6E6E6
// activity_main.xml — references @color/ once, works for both modes
Testing steps:
1. Run the app with the device/emulator in light mode — background
should be near-white, text near-black.
2. Toggle system dark mode (emulator settings, or the quick-settings
tile) without restarting the app.
3. The background and text colors should switch to the values-night
versions automatically, with no code change and (in most cases) no
app restart required.
Notes:
- Both colors.xml files define colors with the EXACT SAME names
(background, text_primary) — the layout XML never references
"values" or "values-night" directly, it just says @color/background,
and Android resolves which file's definition applies based on the
active system setting.
- If values-night/colors.xml were missing a color that values/colors.xml
defines, Android falls back to the light-mode (default values/)
definition for that specific color — values-night only needs to
override the colors that actually differ.
- This only works because the app's theme has a DayNight parent
(Theme.MaterialComponents.DayNight, from Challenge 2) — without that,
values-night resources would simply never be selected.