Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 700
Nick Mowen Avatar asked Feb 25 '16 14:02

Nick Mowen


People also ask

What is DayNight theme Android?

The content is correct as-of April 26th 2019. The DayNight functionality in AppCompat allows your app to easily switch between a dark ⚫ and light ⚪ theme. This has many benefits for your users, from saving power on OLED displays, to increasing usability for people with reduced-vision, and more.

What is theme AppCompat DayNight?

AppCompat. DayNight. DayNight theme allows to switch between light(day) and dark(night) themes, based on the time. Keep in mind that it supports API 14+. Any lower and it defaults to the Light theme.


1 Answers

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>
like image 148
Gabriele Mariotti Avatar answered Oct 11 '22 19:10

Gabriele Mariotti