17

Starting to get my hands into that new update to the Google Support Library and I want to implement the Theme.AppCompat.DayNight into my app.
The problem I am having is that it seems no one explained how to customize it. So If I want to have a different colorAccent for day and a different one for night, how do I do that? Are you supposed to specify different dark and light themes to base off of? Thanks in advance!

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
Nick Mowen
  • 2,572
  • 2
  • 22
  • 38

1 Answers1

39

You can use the night resource qualifier folder.
In this way you can define colors and the other resources for the dark (night) and for the light theme (day).

Qualifiers:
night: Night time
notnight: Day time

In order to support the dark theme with a Material Components Theme use:

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    <!-- ... -->
</style>

With an AppCompat theme:

  <style name="AppTheme" parent="Theme.AppCompat.DayNight">
      <item name="colorPrimary">@color/primary</item>
   </style>

Then define in your app theme the references color resources, and override the value in the values-night directory if needed:

Example: res\values\colors.xml:

   <color name="colorPrimary">.....</color>

In res\values-night\colors.xml folders define the same color:

   <color name="colorPrimary">.....</color>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • Ok, trying to set this up but I am having an issue. Even when I set the night mode to on, the theme still shows as light. Do I have to manually create the dark mode? – Nick Mowen Feb 26 '16 at 00:49
  • @JoeSmith Take a look here: https://medium.com/@chrisbanes/appcompat-v23-2-daynight-d10f90c83e94#.eyx8h9u26 – Gabriele Mariotti Feb 26 '16 at 06:23
  • @JoeSmith You can also read this http://stackoverflow.com/questions/35640951/appcompat-daynight-theme-always-appears-as-a-light-theme – Gabriele Mariotti Feb 26 '16 at 07:57
  • In my app, I've tried with `uiModeManager.setNightMode(UiModeManager.MODE_NIGHT_YES);` and it works perfectly on my Nexus 5 with Android 6 on an emulator with Android 6, but it doesn't work on emulator with Android 5.0.. Why? – Simone Sessa Mar 04 '16 at 17:45
  • What if i need to have a drawable selector? Do I have to create drawable-night and drawable-notnight respectively? – ThanosFisherman Mar 11 '16 at 11:26
  • @ThanosF yes. for example: **drawable-night-hdpi** (night must go before resolution) – prompteus May 04 '16 at 19:23