30

for example:

I have a string in the resources:

<string name="smth"><small>hey girls</small></string>

When I use it in the xml resource files (for example in some text view),

android:text="@string/smth"

no problem whatsoever. It takes into account the "small" tag. It makes the string smaller. But when I want to use it like this:

String smth = getString(R.string.smth);

someTextView.setText(Html.fromHtml(smth));

the string doesn't have any tags!!!

Any help appreciated. Dan

5 Answers 5

48

Use HTML tags with escaped entities.

Your question is answered directly in 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. For example:

Store your styled text resource as an HTML-escaped string:

<resources>
  <string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>
</resources>

In this formatted string, a <b> element is added. Notice that the opening bracket is HTML-escaped, using the &lt; notation. Then format the string as usual, but also call fromHtml(String) to convert the HTML text into styled text:

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);
Sign up to request clarification or add additional context in comments.

5 Comments

This appears to no longer work. When I change <b> to &lt;b> I get an XML build error.
It appears the XML is very picky and you have to use positional parameters. If you just use %s and let String.format() assign them in order you will get a XML build error.
You quoted the wrong passage, because above, it says that you need not escape anything: stackoverflow.com/a/18199543/89818
This worked nicely. Thanks. Instead of using a separate String.format(...) I used res.getString(R.string.welcome_messages, username, mailCount) with success.
Works perfectly. Would recommend replacing Html.fromHtml(text) with HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_COMPACT) if you're using the androidx.core library so as to ensure compatibility with os version <24
14

Why not try to replace '<', '>' and '\' by the corresponding unicode characters?

Regards.

1 Comment

Why not put it into a CDATA section?
10

If You want html tags not to remove by getString(). You have to add a and [![CDATA]] tag in your string like:

<string name="get_string_with_Html_tags"><Data><![CDATA[ This is an example.
        <br><br>
        <font color=\'black\'>black text:
        </font>
        <br><br>
        <font color=\'red\'>red text</font>
        <br></br> ]]> </Data></string>

Comments

9

Sometimes only unicode characters or <![CDATA[ your HTML ]]> helps but not quotes as Rajkiran suggested. Also see How to make Resources.getStringArray work with HTML markup in Android

Comments

-1

Simply put your string in double quotes " ". So that everytime you don't need to put the corresponding unicode characters and can simply use the original text just surrounded with double quotes. Putting quotes around the text means that, the contents inside are just a part of plain text and not aimed to be parsed.

e.g. <string name="smth">"<small>hey girls</small>"</string>

This problem occurs because, tags follow HTML conventions, so whenever you write something between < & > it is considered as tag. And android tries to parse it. And you get error since its not parsable in strings.xml.

1 Comment

Does not work for me. getString() strips out tags even if the string is quoted

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.