CocoNess CocoNess - 8 months ago 52
Android Question

format statement in a string resource file

I have strings defined in the usual strings.xml Resource file like this:

<string name="hello_world"> HELLO</string>

Is it possible to define format strings such as the one below

result_str = String.format("Amount: %.2f for %d days ", var1, var2);

in the strings.xml resource file?

I tried escaping the special characters but its not working.

Thanks in advance


You should add formatted="false" to your string resource

Here is an example

In your strings.xml :

<string name="all" formatted="false">Amount: %.2f%n  for %d days</string>

In your code:

yourTextView.setText(String.format(getString(R.string.all), 3.12, 2));