Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android XML Percent Symbol

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 
like image 870
zaid Avatar asked Dec 11 '10 00:12

zaid


1 Answers

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.

like image 167
Josef Pfleger Avatar answered Oct 13 '22 01:10

Josef Pfleger