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 !
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 withfromHtml(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"><b>Region:</b> %1$s</string>
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));
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..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With