6

I have a string in my resource:

<string name="region"><b>Region:</b> %1$s</string>

I am trying to set this text to a textview:

tvRegion.setText(Html.fromHtml(R.string.region, "France"));

The problem is that the string "region" is not bold !

1
  • 1
    Can you check if returned String is correct? Maybe b is escaped? Commented Aug 8, 2014 at 10:42

3 Answers 3

8

From the official documentation:

Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won't work because the String.format(String, Object...) method will strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place.

You need to HTML-escape the opening bracket of html tags, when inserting it in any xml resource file.

<string name="region">&lt;b>Region:&lt;/b> %1$s</string>
Sign up to request clarification or add additional context in comments.

Comments

5

Here's how you can do that in xml:

<string name="region"><![CDATA[<b>Region:</b> %1$s]]></string>

And the java code:

String str = getString(R.string.action_about_msg, "France");
tvRegion.setText(Html.fromHtml(str));

Comments

1

You can use HTML text directly in Html.fromHtml() method.. try this way..

tvRegion.setText(Html.fromHtml("<b>" + yourtext+ "</b>" + "Other text.."));

with this you can style your string with HTML tags.. as you want..

Comments

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.