0

I have this string:

<string name="order_summary_name">Name: <xliff:g id="name" example="Thomas">%s</xliff:g></string>

using it in this format:

priceMessage=getString(R.string.order_summary_name,name);

and I keep getting an error that says: Format string 'order_summary_name' is not a valid string.

I cant find the mistake,what should I do ???

2 Answers2

0

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 element is added. Notice that the opening bracket is HTML-escaped, using the < notation.

Then format the string as usual, but also call fromHtml(String) to convert the HTML text into styled text:

String text = getString(R.string.welcome_messages, username, mailCount);
String styledText = Html.fromHtml(text);

(or)

  String styledText = Html.fromHtml(R.string.order_summary_name,name);

https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
0

The id attribute is just used to identify what the substitution parameter represents (in your case, it represents the quantity). It's as you said, a note, and not actually used programmatically.

That said, in Android Studio, if you have Code Folding enabled for strings, it will substitute in the ID when it shows the collapsed string. You'd see something like this:

Try this.

mTextView.setText(getString(R.string.order_summary_name, "Thoma"));
Ninja
  • 678
  • 10
  • 26