format statement in a string resource file
Asked Answered
H

6

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.

Hubsher answered 27/9, 2012 at 18:2 Comment(1)
You can try in below way: mTextView.setText(String.format("Score: "+"%1$s", runs)); where int runs = 100;Retired
H
321

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);
Haemophilic answered 2/1, 2014 at 16:55 Comment(4)
I get java.util.IllegalFormatConversionException: %d can't format java.lang.Double arguments when I use $d think $d is an integerSteadfast
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.htmlHaemophilic
res.getString(R.string.welcome_messages, username, mailCount) will do the job. The getString method of resource/context includes the formatting functionality.Mind
Best and straightforward solution.Weaken
S
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));
Sequestration answered 27/9, 2012 at 18:15 Comment(8)
Not necessary according to the docs for Formatting strings. developer.android.com/guide/topics/resources/…Cathrinecathryn
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 https://mcmap.net/q/135956/-format-statement-in-a-string-resource-fileHaemophilic
Note that you can simplify String.format(getString(R.string.all), 3.12, 2) this way: getString(R.string.all, 3.12, 2).Constantan
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.Premeditate
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%"Jitterbug
How to fix this problem if string.xml is in library and i can not recompile it ?Quillon
@VytautasBerankis If you redefine the string in your project, it should be overriddenSequestration
I will try but there are over 300 strings :/Quillon
G
16

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

Gabbi answered 14/3, 2016 at 11:4 Comment(0)
N
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);
Noranorah answered 7/6, 2014 at 14:51 Comment(0)
C
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

Coffin answered 24/6, 2020 at 14:6 Comment(0)
I
1

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))
Israelite answered 4/2 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.