0

I have an application with different themes accessible. The color of a part of the text may vary depending on the theme. But when I use a color attribute that varies according to the theme, the color is not correct: the text is written in black instead of the desired color.

the set color with "font color="#307CA4"" is ok but with font "color=?color_txt_red" not work. why?

Part of code used:

file string.xml :

<resources> 
    <string name="txt_show" formatted="false"><![CDATA[<font color="#307CA4" ><b>Liste des messages reçus : </b></font>Vous avez <font color=?color_txt_red>%d message </font>non lu]]></string>
</resources>

file color.xml :

<resources>
    <color name="red_1">#ff0000</color>
    <color name="red_2">#c3051f</color>
</resources>

file style.xml :

<attr name="color_txt_red" format="reference|color" />

<style name="theme_1" >
    <item name="color_txt_red">@color/red_1</item>
</style>

<style name="theme_2" >
    <item name="color_txt_red">@color/red_2</item>
</style>

part of file testMain.java:

int nbMessage = 0;
tv.setText(Html.fromHtml(getString(R.string.txt_show, nbMessage)));

image valid image valid red_1

image error image error

May anyone could help me, please? Thank you!

3
  • You can use a spannablestring builder instead Commented Oct 5, 2017 at 9:10
  • @Raghunandan : yes but that means that you have to put several string in place for a sentence because the size of the text to be red is dynamic. There is not a solution to work with a single string? Commented Oct 5, 2017 at 10:20
  • Try this: stackoverflow.com/a/14986834/3998710 Commented Feb 22, 2023 at 7:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.