Home > Back-end >  How to set text color for TextView from theme variable in XML layout?
How to set text color for TextView from theme variable in XML layout?

Time:12-13

If I set color easy way, like this android:textColor=?myThemeVariable, then I getting iflating error. How to resolve that trouble?

CodePudding user response:

If I understood correctly what you are looking for is that your TextView or whatever view to be aligned with your configuration of dark-mode and light-mode, right?

So you'd need to have two style.xml files.

  1. res/drawable/values/themes.xml
  2. res/drawable/values/themes.xml(night)

Inside of each one you need to assign the color as follows :

<item name="android:textColorPrimary">@color/white</item> (night)

<item name="android:textColorPrimary">@color/black</item> (light)

So whenever you want to follow that rule in your TextView you can use ?android:textColorPrimary as follows :

android:textColor = "?android:textColorPrimary"

Another option is instead of using themes you can create two colors.xml

  1. values/colors.xml
  2. values-night/colors.xml

Then there you have the color with the same name but changing the hex, so instead of having what you have

<color name="md_theme_light_text_color">#000000</color>
<color name="md_theme_dark_text_color">#ffffff</color>

On each colors.xml use :

<color name="textColor">#000000</color> //values/colors.xml
<color name="textColor">#ffffff</color> //values-nigh/colors.xml

So in your xml you can use textColor as follows :

android:textColor = "@color/textColor"

  • Related