Reputation: 93
How can I make it so that drawables and objects in MainActivity.xml change colour depending on if dark mode is on or not? I have this in colors.xml:
<resources>
<color name="colorPrimary">#141414</color>
<color name="colorPrimaryDark">#000000</color>
<color name="colorAccent">#C923FE</color>
<color name="textColor">#9719BF</color>
<color name="colorPrimary2">#81CDC9</color>
<color name="colorPrimaryDark2">#A8F8F3</color>
<color name="colorAccent2">#3C3C3C</color>
<color name="textColor2">#6E6E6E</color>
I want the second set of colours to be set in light mode, and the first in dark mode. My styles.xml consists of this:
<style name="AppThemeDark" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:statusBarColor">@color/colorPrimary</item>
<item name="android:textColorHint">@color/textColor</item>
</style>
<style name="AppThemeLight" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimary2</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark2</item>
<item name="colorAccent">@color/colorAccent2</item>
<item name="android:statusBarColor">@color/colorPrimary2</item>
<item name="android:textColorHint">@color/textColor2</item>
</style>
If my drawables and components in MainActivity access their colours with something like
android:textColor="@color/colorAccent"
then is there a way for me to change the pallete of the app if dark mode is on/off? Is there something i'm missing from all the tutorials i've seen, where I have to do something special to the MainActivity.xml components and drawables so they can access colours a different way? Thanks :)
Upvotes: 4
Views: 5579
Reputation: 3449
Remember that Dark Mode
is not Night Mode
. They are completely different. DM was introduce in Android 10 that enforce built-in black and white color while NM was on earlier version that uses either default/defined style depending on your implementation. If you want your app to use your define light/night style and not relying on built-in dark style, you may want to forceDarkAllowed
to false in themes.xml or style.xml as DM may conflict.
Upvotes: -1
Reputation: 440
Not need to duplicate colors and drawables. Just create -night folder like drawable-night and values-night etc.
Then put same files with same name (colors.xml/drawable files).
Sample:
values/colors.xml
<color name="colorDefaultText">#757575</color>
values-night/colors.xml
<color name="colorDefaultText">#FFFFFF</color>
Upvotes: 10