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
You can escape % using %% for XML parser, but it is shown twice in device.
To show it once, try following format:
\%%
For Example
is shown as
Fat Burning (50% to 60%)
in deviceA tricky method: using small percent sign as below
Percent Sign on Wikipedia
to escape the percent symbol, you just need %%
for example :
returns "10%"
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 tofalse
: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-%:Now
aapt
gives you no errors but depending on how you use it, the resulting string can be "%%a + %%a == 2%%a" if aFormatter
is invoked without any format arguments: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.
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 :
DO :
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:
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: