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;
    – Ganesh
    Commented Dec 14, 2015 at 13:38

6 Answers 6

322

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);
4
  • 1
    I get java.util.IllegalFormatConversionException: %d can't format java.lang.Double arguments when I use $d think $d is an integer Commented Apr 17, 2014 at 5:43
  • 7
    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
    – LocalPCGuy
    Commented Apr 17, 2014 at 20:04
  • 2
    res.getString(R.string.welcome_messages, username, mailCount) will do the job. The getString method of resource/context includes the formatting functionality. Commented Nov 13, 2015 at 2:24
  • 1
    Best and straightforward solution. Commented Jul 12, 2023 at 3:43
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
  • 6
    Not necessary according to the docs for Formatting strings. developer.android.com/guide/topics/resources/…
    – Squonk
    Commented Sep 27, 2012 at 18:22
  • 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
    – LocalPCGuy
    Commented Jan 2, 2014 at 16:56
  • 20
    Note that you can simplify String.format(getString(R.string.all), 3.12, 2) this way: getString(R.string.all, 3.12, 2). Commented Apr 20, 2016 at 15:31
  • 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. Commented Jul 25, 2016 at 22:29
  • 4
    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%"
    – LanDenLabs
    Commented Mar 7, 2017 at 21:27
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.)

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

2

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.