Giszmo
Giszmo

Reputation: 2117

Escape multiple “%” characters in Android

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 %%.

Upvotes: 30

Views: 17480

Answers (3)

C.d.
C.d.

Reputation: 9995

Using CDATA may work..

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

Upvotes: 1

Ali Azhar
Ali Azhar

Reputation: 1803

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

Upvotes: 49

Mark D
Mark D

Reputation: 3327

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

<string name="test">100\u0025 foo 40\u0025bar</string>

Upvotes: 25

Related Questions