133

I know I can put escaped HTML tags in string resources. However, looking at the source code for the Contacts application I can see that they have a way of not having to encode the HTML. Quote from the Contacts application strings.xml:

<string name="contactsSyncPlug"><font fgcolor="#ffffffff">Sync your Google contacts!</font> 
\nAfter syncing to your phone, your contacts will be available to you wherever you go.</string>

Unfortunately, when I try something similar (like Hello, <b>World</b>!), getString() returns the string without the tags (I can see that in logcat). Why is that? How can I get the original string, with tags and everything? How is the Contacts application doing it?

Felix
  • 88,392
  • 43
  • 149
  • 167

6 Answers6

223

You can also surround your html in a CDATA block as well and getString() will return your actual HTML. Like such:

<string name="foo"><![CDATA[Foo Bar <a href="foo?id=%s">baz</a> is cool]]></string>

Now when you perform a getString(R.string.foo) the string will be HTML. If you need to render the HTML (with the link as shown) via a clickable TextView you'd need to perform a Html.fromHtml(...) call to get the spannable text.

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
Donn Felker
  • 9,553
  • 7
  • 48
  • 66
  • 1
    No, you should definitely see Felix's answer. CDATA is not necessary. – caw Aug 13 '13 at 02:05
  • 4
    @MarcoW. Felix's answer is true but using CDATA helps us to not to worry about html tags. This answer should be correct answer. – slhddn Feb 08 '14 at 13:52
  • 3
    If you have links in the string, don't forget to add textView.setMovementMethod(LinkMovementMethod.getInstance()); – Adarsh Urs Jan 26 '15 at 11:12
  • 1
    I had to use `\"` for `style` property, though. Example `link` – Fabricio Nov 01 '16 at 10:53
  • 1
    CDATA gives you a lot more flexibility when styling strings with HTML tags. I would agree that is the way to go 100%! – Droid Chris Jun 27 '17 at 18:29
  • `TextView tv = (TextView) findViewById(R.id.textHolder); tv.setText(Html.fromHtml(getString(R.string.foo))); tv.setMovementMethod(LinkMovementMethod.getInstance());` – user25 May 26 '18 at 22:32
  • Also, `getText()` doesn't accept format arguments. If you have something other than a static string you must use `getString()` + `Html`. – Jeffrey Blattman Sep 18 '19 at 18:20
  • Use this ink : https://developer.android.com/guide/topics/resources/string-resource#StylingWithHTML – pravingaikwad07 Jul 14 '21 at 19:02
99

It seems getString() does just that -- gets a string. To use this, you have to use getText() (and no more Html.fromHtml()), i.e.:

mTextView.setText(getText(R.string.my_styled_text));

However, it seems the android:text property does just the same thing, and the following is equivalent:

<TextView android:text="@string/my_styled_text" />

And in strings.xml:

<string name="my_styled_text">Hello, <b>World</b>!</string>
Felix
  • 88,392
  • 43
  • 149
  • 167
  • This works great, but just something that set me back for a while - if using Eclipse, these tags may not display properly in the visual layout editor. (They will display when running on a device or emulator.) – sam Jul 06 '15 at 11:15
  • 1
    Unfortunatly using this method, variables in string are not allowed – Alessandro Muzzi May 12 '16 at 09:40
  • 1
    is supported at api23, but api10 is not. – illusionJJ Jan 19 '17 at 07:14
  • Works for me. Thanks! – JamesD Aug 22 '17 at 14:06
  • Check the link for detailed explanation about the above method.https://stackoverflow.com/questions/45647928/what-is-the-difference-between-getstring-and-gettext. – Kaveri Aug 06 '18 at 05:51
  • Seems the [docs](https://developer.android.com/guide/topics/resources/string-resource#StylingWithHTML) have been updated since last activity here, but the documented tags still don't seem to align with the [StringBlock.java](https://github.com/android/platform_frameworks_base/blob/android-4.2.2_r1/core/java/android/content/res/StringBlock.java#L161) code. Can anyone clarify? – Tim Nov 12 '21 at 09:44