Alexei
Alexei

Reputation: 15726

How do I reference a color from a string resource to format my text?

My strings.xml contains the following string resource

<string name="contacts"><font fgcolor="#FF00FFFF">+</font> Contacts</string>

It is working fine and sets the textcolor to #FF00FFFF

But if I try to reference a color

<string name="contacts"><font fgcolor="@color/strings_font_fgcolor_cyan">+</font> Contacts</string>

it does not work.

I have added the color

<color name="strings_font_fgcolor_cyan">#FF00FFFF</color>

How I can reference this color from my string resource?

Upvotes: 4

Views: 774

Answers (1)

David Medenjak
David Medenjak

Reputation: 34542

You can't reference a color resource value from within your strings. You will have to keep using fgcolor="#FF00FFFF" or format your text at runtime.


Everything between <string name="contacts"> and </string> is treated as your text and it is not processed any further.

If you want to use your resource color, you will have to do this at runtime, by parsing / replacing parts in your String with the loaded value, or manually adding the right tags to it.

Upvotes: 3

Related Questions