Android XML Percent Symbol
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
.
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.
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
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.