Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

percentage symbol in strings.xml

I'm trying to format a string from string.xml to reuse it with several Values. I'm running into some problems as the String should also contain the percentage-symbol which is used by the formatter. I already tried to replace the % symbol by its Unicode presentation but that doesn't seem to work:

<?xml version="1.0" encoding="utf-8"?> <resources>     <string name="teststring">The new value is %1$s%</string>     <string name="teststring2">The new value is %1$s\u0025</string>     <string name="teststring3">The new value is %1$s</string> </resources> 

Java Code:

String value = "25"; String formattedString = context.getResources().getString(R.string.teststring), value); 

In this example, teststring and teststring2 will produce an error, while teststring3 works fine. What's the correct way to put that % symbol in the xml-string to get "The new value is 25%" as formatted String?

Stacktrace:

FATAL EXCEPTION: main java.util.UnknownFormatConversionException: Conversion:      at java.util.Formatter$FormatSpecifierParser.unknownFormatConversionException(Formatter.java:2304)     at java.util.Formatter$FormatSpecifierParser.advance(Formatter.java:2298)     at java.util.Formatter$FormatSpecifierParser.parseConversionType(Formatter.java:2377)     at java.util.Formatter$FormatSpecifierParser.parseArgumentIndexAndFlags(Formatter.java:2348)     at java.util.Formatter$FormatSpecifierParser.parseFormatToken(Formatter.java:2281)     at java.util.Formatter.doFormat(Formatter.java:1069)     at java.util.Formatter.format(Formatter.java:1040)     at java.util.Formatter.format(Formatter.java:1009)     at java.lang.String.format(String.java:1988)     at android.content.res.Resources.getString(Resources.java:343)     at (... and so on) 

The correct answer is

<string name="teststring">The new value if %1$s%%</string> 

formatted="false" must not be set.

like image 290
danijoo Avatar asked May 30 '13 10:05

danijoo


People also ask

How to write percent in XML?

If you are accessing from <string-array/>, then for printing percentage use \u0025.

What does a percent sign mean in XML?

The percent sign tells the XML processor to look up the entity name in the DTD's list of parameter entities, insert the value of the entity into the DTD in place of the entity reference, and process the value of the entity as part of the DTD.

How do you put a percent sign in a string?

You can do this by using %% in the printf statement. For example, you can write printf(“10%%”) to have the output appear as 10% on the screen.

What is string in XML?

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.


2 Answers

Use

<?xml version="1.0" encoding="utf-8"?> <resources>     <string name="teststring">The new value is %1$s%%</string>  </resources> 

In java

String value = "25"; String formattedString =     String.format(getResources().getString(R.string.teststring), value); Log.i("",formattedString); 
like image 69
Arun C Avatar answered Sep 20 '22 05:09

Arun C


Can you try \%% instead of % ??

Like,

<string name="teststring">The new value is \%%</string> 

Or

<string formatted="false" name="teststring" >The new value is %</string> 
like image 20
Basim Sherif Avatar answered Sep 22 '22 05:09

Basim Sherif