Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: string format with Double value in Strings.xml

Tags:

android

I need to create a String using the Formater to display some double values in application. I'm not clear on how to code it. Here is what I have:

<string name="diseaseMessage">You have %s message, Unread %s</string>

i get this error:

error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?  
error: Unexpected end tag string    strings.xml /TSMS Pro/res/values    line 70 Android AAPT Problem
like image 721
DolDurma Avatar asked Feb 26 '15 10:02

DolDurma


People also ask

How do you make a string bold in xml?

Just use getText(R. string.

How do you round off double value in Android?

You can use String. format("%. 2f", d) , your double will be rounded automatically.

What is string XML file in Android?

String. xml file contains all the strings which will be used frequently in Android project. String. xml file present in the values folder which is sub folder of res folder in project structure.In Android Studio, we have many Views such as TextView,Button,EditText,CheckBox,RadioButton etc.

Should Use @string resource in Android Studio?

You should add them to a string resource file and then reference them from your layout. This allows you to update every occurrence of the word "Yellow" in all layouts at the same time by just editing your strings. xml file. It is also extremely useful for supporting multiple languages as a separate strings.


1 Answers

<string name="diseaseMessage">You have %1$s message, Unread %2$s</string>

From the Android docs:

Formatting strings

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);
like image 172
Mr. Kevin Thomas Avatar answered Sep 22 '22 13:09

Mr. Kevin Thomas