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!
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.
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.
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 timenotnight
: 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With