16

In my app, I'd like to define a format string in strings.xml that looks like this (note the <b>...</b> tags):

<string name="location"><b>Location:</b> %1$s</string>

And then use getString(int, Object...) to substitute in a format argument:

String formattedString = getString(R.string.location, "Edmonton, AB");

This produces a value of "Location: Edmonton, AB". I'd like to get a value of "<b>Location:</b> Edmonton, AB".

Is there some way of doing this using string formats in strings.xml without splitting it up into two strings?

2 Answers 2

21

From the docs:

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:

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

Make sure to escape the text you are passing in String.format()

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

2 Comments

@GregInYEG - Happens to the best of us. My pleasure.
Just be aware of the fact that TextUtils.htmlEncode() method does not replace ' ' (space) with '&#160;'. This is causing troubles in my case (I'm using mailto: hyperlink with subject and body parameters). Otherwise the solution above works great.
1

Use String.Format. e.g.

  <string name="location"><![CDATA[<b>Location:</b> %s]]></string>

String formattedString = String.Format(getString(R.string.location), "Edmonton, AB");

4 Comments

The string in your example doesn't contain styles. If it did, the call to getString(R.string.location) would strip them out.
I thought the point was to format a string which is what my example does. Add the format characters as extra parameters to the format method.
String.format(getString(R.string.location), "Edmonton, AB") returns the same thing as getString(R.string.location, "Edmonton, AB"). Neither preserve styles like <b>...</b> in the string from strings.xml. @iPaulPro's answer points to the android documentation that explains if I want to use both styles and format arguments, I need to escape the styles and use Html.fromHtml(...).
You need to escape your string resources. I have amended my answer above. (Did not realise the Html codes were the point of the question)

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.