28

In <string-array name="versions"> I have this beast of an entry (boiled down to a reasonable minimum to reproduce the effect):

<item>100% foo 40%bar</item>

which produces these errors:

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

Adding formatted="false" doesn't change a thing.

<item>100&#37; foo 40&#37;bar</item>

results in the same error messages. WTH?

<item>100% foo 40bar</item>
<item>100 foo 40%bar</item>
<item>100% foo 40%</item>

would all work fine. Escaping it with \% is just ignored resulting in the same error. %% doesn't result in an error but I get %%.

8
  • stackoverflow.com/questions/4414389/android-xml-percent-symbol/… , most of that you say won't work, but see the last option. Feb 22, 2012 at 0:23
  • Thanx for the answer. Unfortunately I am not dealing with a string that might allow substitution at all unless there is a way to do substitutions in arrays. In my arrays.xml there is a string-array with items. These I load via getResources().getStringArray(R.array.versions);
    – Giszmo
    Feb 22, 2012 at 8:07
  • ouch, getStringArray uses the formatter and yet doesn't allow you to pass format arguments. I'm afraid that you'll have to recreate the array after you load it, substiting in the %, or else correct the strings as you use them. Feb 22, 2012 at 8:22
  • Java strings are immutable. The methods return copies. Feb 22, 2012 at 11:05
  • 1
    haha collapsar :) I like your idea but I settled with %% and subsequent string replacement. So I can't pick your solution for others that run into the same issue?
    – Giszmo
    Mar 2, 2012 at 0:58

3 Answers 3

47

The % is a reserved character in XML like <, >, etc. Use %% for each % you are using in the string resource.

4
  • 3
    You are slightly late with your reply but thanks anyway :D I might test your solution if I come accross Android development soon but given I had tried &#37; I though I had done enough of escaping back then. \%% is definitely nicer than \u0025 … well … maybe not "definitely". :)
    – Giszmo
    Jun 4, 2015 at 0:17
  • 4
    %% was the only one that work for me. This is my final string (%1$s%% commission) that translates, for example, in (2.56% commission)
    – Laranjeiro
    Jul 28, 2015 at 9:23
  • 8
    % most certainly is not a reserved character in XML; this content restriction is specific to Android resource files.
    – Cumbayah
    Mar 6, 2016 at 21:04
  • Thanks, save my hours
    – Ho Luong
    Aug 2, 2020 at 16:09
25

Encoding each as a unicode character in the xml works for me:

<string name="test">100\u0025 foo 40\u0025bar</string>
3
  • 1
    Ok, I guess this should be the way to go. Still I would like to understand why both &#37; and \u0025 are % but version 1 makes me run into the error??
    – Giszmo
    Mar 22, 2012 at 16:12
  • 3
    I lost one good 20 minutes on this, so I hope can save time for someone else. Having String to format, I had to use e.g., Score: <b>%.2f%%</b>. Two % character, attached to the format. None of the above worked in this case. Mar 11, 2014 at 11:41
  • 5
    I would not recommand replacing % with unicode character. They are encoded but not escaped. If you have parameter within your sentence like <string name="test">Value in percent : %1$d \u0025</string> and call " int testValue = 29; String text = getResources().getString(R.string.test, testValue);" your application will only crash... Instead use %% (or eventually <string name="test">Value %1$d \u0025\u0025</string> would work but really dirty)
    – ClemM
    Sep 5, 2016 at 17:32
1

Using CDATA may work..

<item><![CDATA[100% foo 40%]]></item>
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.