182

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.

1
  • 1
    You can try in below way: mTextView.setText(String.format("Score: "+"%1$s", runs)); where int runs = 100; Commented Dec 14, 2015 at 13:38

6 Answers 6

325

You do not need to use formatted="false" in your XML. You just need to use fully qualified string format markers - %[POSITION]$[TYPE] (where [POSITION] is the attribute position and [TYPE] is the variable type), rather than the short versions, for example %s or %d.

Quote from Android Docs: String Formatting and Styling:

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

In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal integer. You can format the string with arguments from your application like this:

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

4 Comments

I get java.util.IllegalFormatConversionException: %d can't format java.lang.Double arguments when I use $d think $d is an integer
Here is a list of all of the different convertors, you'll have to choose the appropriate one for the number type, you may need %f (for floating point): docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html
res.getString(R.string.welcome_messages, username, mailCount) will do the job. The getString method of resource/context includes the formatting functionality.
Best and straightforward solution.
110

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));

8 Comments

Not necessary according to the docs for Formatting strings. developer.android.com/guide/topics/resources/…
That is one way to fix, but might be confusing to some because formatted="false" could imply the string does not get formatted. Posted another solution stackoverflow.com/a/20887690/228429
Note that you can simplify String.format(getString(R.string.all), 3.12, 2) this way: getString(R.string.all, 3.12, 2).
There is a major problem with all the answers in this page: If you need to add a percent sign right next to a param, say 25%, Android will crash.
You can escape the percent sign by including two %%. This following standard formatting syntax. Example: String.format("Percent %d%% or as float %.2f%%", 12, 12.34f); Will generate a string "Percent 12% or as float 12.34%"
|
17

Inside file strings.xml define a String resource like this:

<string name="string_to_format">Amount: %1$f  for %2$d days%3$s</string>

Inside your code (assume it inherits from Context) simply do the following:

 String formattedString = getString(R.string.string_to_format, floatVar, decimalVar, stringVar);

(In comparison to the answer from LocalPCGuy or Giovanny Farto M. the String.format method is not needed.)

Comments

9

Quote from Android Docs:

If you need to format your strings using String.format(String, Object...), then you can do so by putting your format arguments in the string resource. For example, with the following resource:

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

In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguments from your application like this:

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);

Comments

4

For me it worked like that in Kotlin:

my string.xml

 <string name="price" formatted="false">Price:U$ %.2f%n</string>

my class.kt

 var formatPrice: CharSequence? = null
 var unitPrice = 9990
 formatPrice = String.format(context.getString(R.string.price), unitPrice/100.0)
 Log.d("Double_CharSequence", "$formatPrice")

D/Double_CharSequence: Price :U$ 99,90

For an even better result, we can do so

 <string name="price_to_string">Price:U$ %1$s</string>

 var formatPrice: CharSequence? = null
 var unitPrice = 199990
 val numberFormat = (unitPrice/100.0).toString()
 formatPrice = String.format(context.getString(R.string.price_to_string), formatValue(numberFormat))

  fun formatValue(value: String) :String{
    val mDecimalFormat = DecimalFormat("###,###,##0.00")
    val s1 = value.toDouble()
    return mDecimalFormat.format(s1)
 }

 Log.d("Double_CharSequence", "$formatPrice")

D/Double_CharSequence: Price :U$ 1.999,90

Comments

3

Feb 2024
Simpley define placeholders in the resource file:
1. Use fully qualified placeholders of the form %[POSITION]$[TYPE] within your string resource.

  • POSITION indicates the argument's position (e.g., 1 for the first argument).
  • TYPE specifies the expected argument type (e.g., s for string, d for decimal).
    Here is a list of the type specifiers and their meanings.[full list]
Conversion Description
b Boolean
c Character
s String
d Signed Integer
e Float in Scientific Notation
f Float in Decimal Format
g Float in Decimal or Scientific Notation, depending on value
h Hashcode of the supplied argument
o Octal Integer
x Hexadecimal Integer
t Date or Time

example :

<string name="welcome_message">Hello, %1$s! You have %2$d new messages.</string>

2. Use methods like getString() to retrieve the string resource, providing the necessary arguments for formatting.

Java :

String username = "Alice";
int messageCount = 5;
String formattedMessage = getString(R.string.welcome_message, username, messageCount);

Kotlin :

val username = "Alice"
val messageCount = 5
val formattedMessage = getString(R.string.welcome_message, username, messageCount)

Jetpack Compose :

val username = "Alice"
val messageCount = 5
Text(text = stringResource(id = R.string.welcome_message, username,messageCount))

Comments

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.