Android XML Percent Symbol
Asked Answered
N

14

350

I have an array of strings in which the % symbol is used. Proper format for using the % is %. When I have a string in that array with multiple % it gives me this error.

 Multiple annotations found at this
 line:
 - error: Multiple substitutions specified in non-positional format;
   did you mean to add the formatted="false" attribute?
 - error: Found tag </item> where </string-array> is expected
Namedropper answered 11/12, 2010 at 0:13 Comment(4)
Can you post the XML that's causing this error? It can be very difficult to identify a problem without seeing it's cause.Sewerage
this is an error that can easily be replicated by any string with 2 or more % symbols.Namedropper
If not looking for formatting purpose, better way to accomplish.Heiney
In my case, I wrongly entered the formatting param as 1%$s, instead of %1$s.Olden
D
501

The Android Asset Packaging Tool (aapt) has become very strict in its latest release and is now used for all Android versions. The aapt-error you're getting is generated because it no longer allows non-positional format specifiers.

Here are a few ideas how you can include the %-symbol in your resource strings.

If you don't need any format specifiers or substitutions in your string you can simply make use of the formatted attribute and set it to false:

<string formatted="false">%a + %a == 2%a</string>

In this case the string is not used as a format string for the Formatter so you don't have to escape your %-symbols. The resulting string is "%a + %a == 2%a".

If you omit the formatted="false" attribute, the string is used as a format string and you have to escape the %-symbols. This is correctly done with double-%:

<string>%%a + %%a == 2%%a</string>

Now aapt gives you no errors but depending on how you use it, the resulting string can be "%%a + %%a == 2%%a" if a Formatter is invoked without any format arguments:

Resources res = context.getResources();

String s1 = res.getString(R.string.str);
// s1 == "%%a + %%a == 2%%a"

String s2 = res.getString(R.string.str, null);
// s2 == "%a + %a == 2%a"

Without any xml and code it is difficult to say what exactly your problem is but hopefully this helps you understand the mechanisms a little better.

Disapprove answered 11/12, 2010 at 15:6 Comment(9)
i selected this as the answer because it should work, but apparently there is a bug that im running into with that string, so ive decided to use %% and "XXX.replaceAll("%%", "%");"Namedropper
using formatted=false works nicely. This is how I used it: <string name="information_description" formatted="false">\'Sweet\' 10% to 20% even 35% sugar by weight</string>Moreta
formatted=false works nicely for me too. Using %%, it literally showed up as %% on my device and emulator..Premise
formatted=false worked fine for me in the past. now (ADT 22) , for some reason it doesn't. and if i use : " str=mContext.getString(R.string.str,"hello") " , it will have this lint warning: "Format string 'str' is not a valid format string so it should not be passed to String.format"Alenaalene
Forget it, I just needed to clean the projectNetwork
If not for formatting purpose, this answer provides best way to do display any number of % symbols in view using XML and string resources.Heiney
You should always use formatted="false" when using String.format.Neu
If you are accessing from <string-array/>, then for printing percentage use \u0025. This worked for me.Crelin
As per the answer from Paul E, on SDK 22 I had to add the dollars symbol to apply a format: %1$s and %2$dCadenza
O
163

To allow the app using formatted strings from resources you should correct your xml. So, for example

<string name="app_name">Your App name, ver.%d</string>

should be replaced with

<string name="app_name">Your App name, ver.%1$d</string>

You can see this for details.

Obvert answered 25/2, 2011 at 6:44 Comment(2)
For multiple parameter substitution (e.g. with strings), use %1$s, %2$s etc.Cero
who will read such a big answer for this little thing? Thanks, I scroll down to find you.Tagalog
F
122

You can escape % using %% for XML parser, but it is shown twice in device.

To show it once, try following format: \%%

For Example

<string name="zone_50">Fat Burning (50\%% to 60\%%)</string> 

is shown as Fat Burning (50% to 60%) in device

Fredette answered 7/10, 2011 at 17:46 Comment(4)
\%% causes crashes on Nexus 4 4.2.1. Haven't tested others.Nicholenicholl
This works well and is easy when you already know the percent you want to include into your strings, versus using string formatter and always having to set those hardcoded values programmatically.Elielia
This crashed for me as well on Nexus 5.1.1. @ViliusK answer is the correct one.Ornithomancy
just use %% instead of %Sidwel
T
59

Use

<string name="win_percentage">%d%% wins</string>

to get

80% wins as a formatted string.

I'm using String.format() method to get the number inserted instead of %d.

Translate answered 31/3, 2015 at 16:22 Comment(0)
R
20

to escape the percent symbol, you just need %%

for example :

String.format("%1$d%%", 10)

returns "10%"

Rangy answered 11/4, 2016 at 14:13 Comment(2)
Your answer is pretty much the same as several other existing answersWherefore
... and wrong, because returns 10% not %10. FIY: When you use String.format you should everytime specify Locale. You can be really surprised when you don't put Locale and use Arabic locale for format numbers...Och
A
15

In your strings.xml file you can use any Unicode sign you want.

For example, the Unicode number for percent sign is 0025:

<string name="percent_sign">&#x0025;</string>

You can see a comprehensive list of Unicode signs here

Alpestrine answered 29/4, 2013 at 7:24 Comment(2)
That does not make any sense. &#x0025; and % are identical in a xml file.Bellerophon
The resource file is converted into a Java resource, and in doing so, the % character returns. I'm afraid this doesn't resolve the problem.Inclusive
V
9

You can escape the % in xml with %%, but you need to set the text in code, not in layout xml.

Vapor answered 15/3, 2011 at 8:54 Comment(1)
Yes it does escape with double %, and compile nicely. But when run on emulator, or even devices, it could show up as %%.Premise
G
8

Not exactly your problem but a similar one.

If you have more than one formatting in your string entry, you should not use "%s" multiple times.

DON'T :

<string name="entry">Planned time %s - %s (%s)</string>

DO :

<string name="entry">Planned time %1$s - %2$s (%3$s)</string>
Gnathion answered 3/1, 2017 at 8:39 Comment(0)
T
6

A tricky method: using small percent sign as below

 <string name="zone_50">Fat Burning (50&#65130; to 60&#65130;)</string> 

Percent Sign on Wikipedia

Telophase answered 29/5, 2015 at 7:40 Comment(0)
T
5

This could be a case of the IDE becoming too strict.

The idea is sound, in general you should specify the order of substitution variables so that should you add resources for another language, your java code will not need to be changed. However there are two issues with this:

Firstly, a string such as:

You will need %.5G %s

to be used as You will need 2.1200 mg will have the order the same in any language as that amount of mass is always represented in that order scientifically.

The second is that if you put the order of variables in what ever language your default resources are specified in (eg English) then you only need to specify the positions in the resource strings for languages the use a different order to your default language.

The good news is that this is simple to fix. Even though there is no need to specify the positions, and the IDE is being overly strict, just specify them anyway. For the example above use:

You will need %1$.5G %2$s

Teevens answered 28/7, 2014 at 22:14 Comment(0)
D
3

Try this one (right):

<string name="content" formatted="false">Great application %s  ☞  %s  ☞  %s \\n\\nGo to download this application %s</string>
Dossier answered 4/1, 2013 at 17:51 Comment(0)
T
1

Per google official documentation, use %1$s and %2$s http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

Hello, %1$s! You have %2$d new messages.

Tissue answered 11/3, 2016 at 0:58 Comment(0)
A
1

I had a bit different case. I was generating my own XML file from localization website and I could not make lint check work without displaying double %% sign in device. I received lint error for using percent sign without using String.format

I replaced all non formatting % signs to: \u0025

Aylward answered 7/2, 2023 at 10:5 Comment(0)
P
-19

Try using a backslash in front of it, like below:

\%
Plate answered 11/12, 2010 at 0:19 Comment(1)
This solution leads to crashing .Marxist

© 2022 - 2024 — McMap. All rights reserved.