Set Textview Text from Html-Formatted String Resource in Xml

Set TextView text from html-formatted string resource in XML

Just in case anybody finds this, there's a nicer alternative that's not documented (I tripped over it after searching for hours, and finally found it in the bug list for the Android SDK itself). You CAN include raw HTML in strings.xml, as long as you wrap it in

<![CDATA[ ...raw html... ]]>

Edge Cases:

  • Characters like apostrophe ('), double-quote ("), and ampersand (&) only need to be escaped if you want them to appear in the rendered text AS themselves, but they COULD be plausibly interpreted as HTML.
    • ' and " can be represented as\' and \", or ' and ".
    • < and > always need to be escaped as < and > if you literally want them to render as '<' and '>' in the text.
    • Ampersand (&) is a little more complicated.
      • Ampersand followed by whitespace renders as ampersand.
      • Ampersand followed by one or more characters that don't form a valid HTML entity code render as Ampersand followed by those characters. So... &qqq; renders as &qqq;, but <1 renders as <1.

Example:

<string name="nice_html">
<![CDATA[
<p>This is a html-formatted \"string\" with <b>bold</b> and <i>italic</i> text</p>
<p>This is another paragraph from the same \'string\'.</p>
<p>To be clear, 0 < 1, & 10 > 1<p>
]]>
</string>

Then, in your code:

TextView foo = (TextView)findViewById(R.id.foo);
foo.setText(Html.fromHtml(getString(R.string.nice_html), FROM_HTML_MODE_LEGACY));

IMHO, this is several orders of magnitude nicer to work with :-)


August 2021 update: My original answer used Html.fromHtml(String), which was deprecated in API 24. The alternative fromHtml(String,int) form is suggested as its replacement.

FROM_HTML_MODE_LEGACY is likely to work... but one of the other flags might be a better choice for what you want to do.

On a final note, if you'd prefer to render Android Spanned text suitable for use in a TextView using Markdown syntax instead of HTML, there are now multiple thirdparty libraries to make it easy including https://noties.io/Markwon.

HTML in string resource?

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>

How to set formatted html text in a textview from String.xml?

Remove formatted="false" from

  <string name="text_to_show" formatted="false">

or set it to true.

HTML Formatting in TextView

If you format your text in XML it isn't necessary to use Html.formatHtml(...)

Try this:

t6.setText(getString(R.string.tagged_string);

EDIT:
Hmm... This way worked for me fine and I tried exactly your String.
Maybe your Project needs a clean (Project > Clean ...)

If this doesn't help:
I found another question. Maybe the answer there can solve your problem

How to display HTML in TextView?

You need to use Html.fromHtml() to use HTML in your XML Strings. Simply referencing a String with HTML in your layout XML will not work.

This is what you should do in Java

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));
} else {
textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
}

And in Kotlin:

textView.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(html, Html.FROM_HTML_MODE_COMPACT)
} else {
Html.fromHtml(html)
}

android getText from string resource by keeping html tags

If you want to use getText() you have to proceed like this:

    String mystring = activity.getResources().getText(R.string.Terms_And_Conditions).toString()

HTML formatting for TextView

Actually, From the Android Docs..

public final void setText (CharSequence text)

Sets the string value of the TextView. TextView does not accept HTML-like formatting, which you can do with text strings in XML resource files. To style your strings, attach android.text.style.* objects to a SpannableString, or see the Available Resource Types documentation for an example of setting formatted text in the XML resource file.

But,

public final void setText (int resid)
  • no more specification on it..

But from Android Resource String docs..

You can add styling to your strings with HTML markup. For example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="welcome">Welcome to <b>Android</b>!</string>
</resources>

Supported HTML elements include:

<b> for bold text.
<i> for italic text.
<u> for underline text.

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.

And about your URL string,...

tvMyTextView.setText(Html.fromHtml("Click <a href="http://www.poon-world.com">here</a> to switch on the red light.\n"));

Also look at this SO Question Set TextView text from html-formatted string resource in XML

and

Android String Resource



Related Topics



Leave a reply



Submit