Skip to content Skip to sidebar Skip to footer

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 singl

Solution 1:

In fgcolor replace #000000 with black, like

<string name="dialog_text">Text... <font fgcolor="black">TheWordIWantInBlack</font> texttexttext.</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

Solution 2:

Use Spannable String, for example

finalSpannableStringBuildersb=newSpannableStringBuilder("Allow ");
SpannableStrings1=newSpannableString("WhatsApp");
s1.setSpan(newStyleSpan(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.

Post a Comment for "Issue In Changing The Color Of A Word In Strings.xml"