Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change according to dark mode

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 :)

like image 498
Marvil Avatar asked Oct 27 '25 08:10

Marvil


1 Answers

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>
like image 66
utkukutlu Avatar answered Oct 28 '25 21:10

utkukutlu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!