Search code examples
androidandroid-support-libraryandroid-appcompatandroid-theme

How to use the new DayNight Theme?


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!


Solution

  • 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>