Home > other >  How to set color using attr.xml?
How to set color using attr.xml?

Time:10-19

I want to do this:

textView.setTextColor(R.attr.myColor)

This is because I created a custom color for both night mode and light mode

attr.xml
<resources>
    <attr name="myColor" format="reference|color" />

themes.xml
      <item name="textMessageColor">@color/black</item>

themes-night.xml
  <item name="textMessageColor">@color/white</item>

But whenever I use it, it does not work.

CodePudding user response:

Try this in xml like
<TextView
android:id="@ id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="?myColor"/>

CodePudding user response:

You can override the colors that you use in your layout and put them in values-night/colors.xml. So, in the light theme values/colors.xml will be used and in the night theme, values-night/colors.xml will be used.

Let's say you have a color assigned to a button, you write the following line, make sure the color name is the same:

values/colors.xml

<color name="colorButton">#6d85c9</color>

values-night/colors.xml

<color name="colorButton">@color/colorWhite</color>

And then you can assign the color to the button you do normally.

  • Related