Pier
Pier

Reputation: 784

Issue in changing the color of a word in strings.xml

I created a layout for an AlertDialog in which there's a TextView that shows some text. For the text I have an item in strings.xml where I would like to change the color of a single word, just like in the image below. I tried with this:

<string name="dialog_text">Text... <font fgcolor="#000000">TheWordIWantInBlack</font> text text text.</string>

The issue is when I run this on the device (Galaxy Note3) that shows all the text in the dialog except for the word in the <font> tag but on the emulator (Nexus 5 with Android 6.0) everything is ok, exactly how I want it. Could you help me?

enter image description here

Upvotes: 0

Views: 381

Answers (2)

Rick
Rick

Reputation: 4013

In fgcolor replace #000000 with black, like

<string name="dialog_text">Text... <font fgcolor="black">TheWordIWantInBlack</font> text text text.</string>

This only works on a relatively short list of built-in colors: aqua, black, blue, fuchsia, green, grey, lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow

Upvotes: 1

Michael
Michael

Reputation: 3739

Use Spannable String, for example

final SpannableStringBuilder sb = new SpannableStringBuilder("Allow ");
SpannableString s1 = new SpannableString("WhatsApp");
s1.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, s1.length(), 0);
sb.append(s1);

and sb is now "Allow WhatsApp"

In this example, I use type Bold for the styled string, but of course you can customize it by setting font colors and other attributes to your liking.

Upvotes: 0

Related Questions