JPM
JPM

Reputation: 9296

Android string-array display Percent Sign

I have a string-array in my strings.xml file that has multiple items in it. Only one of the items has multiple percetage "%" symbols that needs to be displayed. Seems none of the advice on how to escape it works at all. \%, %% or using formatted="false" on either the string-array or the items tags. When I use \% Eclipses shows errors with the item, if I use %% then Eclipse is fine it runs but then the text displays two percent signs "%%". So any ideas on how to escape % sign in strings.xml?

<string-array name="details">
     <item>Placement of things</item>
     <item>Percentage of times placed is 30% 
         and percentage of times release 50%</item>
</string-array>

Upvotes: 5

Views: 3505

Answers (5)

Maragues
Maragues

Reputation: 38334

What worked for me was declaring a new string in strings.xml with the percent symbols and attribute formatted="false"

<string name="percents" formatted="false">10&#37; is not 20&#37;</string

And then in arrays.xml

<string-array name="my_array">
    <item>@string/percents</item>
    ...

Upvotes: 0

Bao Le
Bao Le

Reputation: 17507

A tricky method is to use unicode character of percent sign

U+FE6A ﹪ small percent sign (HTML &#65130; )
U+FF05 % full-width percent sign (HTML &#65285;)

In XML you can define as following

    <string-array name="details">
         <item>Placement of things</item>
         <item>Percentage of times placed is 30&#65130; 
             and percentage of times release 50&#65130;</item>
    </string-array>

Percent Sign on Wikipedia

Upvotes: 2

j.c
j.c

Reputation: 2882

I have the same issue, but nothing to do, nothing worked for me on JB 4.1.2.
Here's what i tried:

<string-array name="percentage">
    <item>a. 100%</item>            <!-- unknownFormatConversionException -->
    <item>b. 90&#37;</item>         <!-- unknownFormatConversionException -->
    <item>c. 80%%</item>            <!-- "somewhere" a double %% appears -->
    <item>d. 60\%</item>            <!-- no % appears -->
    <item>e. 40\%%</item>           <!-- unknownFormatConversionException -->
    <item>f. 30%\%</item>           <!-- unknownFormatConversionException -->
    <item><![CDATA[g. 20%]]></item> <!-- unknownFormatConversionException -->
    <item formatter="false>h. 10%</item>    <!-- unknownFormatConversionException -->
</string-array>

Using emlators i've seen that before ICS 4.0 everything works great, like in my Galaxy-S GB 2.3.5, while using ICS and above the unknownFormatConversionExceptions appear, just like on my Nexus S JB 4.1.2.
Finally, this was my final decision: omitting percentage symbol at all!

<item>a. 100</item>
<item>b. 90</item>
...

It's just a very rough workaround, but at least i can go on working!

I'm using ADT Build: v22.0.1-685705 under Ubuntu 13.04
testing devices: Nexus S JB 4.1.2, Samsung Galaxy S GB 2.3.5

Upvotes: 1

JPM
JPM

Reputation: 9296

Apparently using an older version of eclipse and not having the latest SDK for android will produce this problem. I upgraded eclipse to 3.6.2 and made sure to install the latest version android sdk r10. So hope this helps someone and saves them time as I wasted a couple of hours trying to fix/find the answer.

So the answer to this question is you can use just a single % sign multiple times in a string array item without having to escape it, double up or use CDATA as long as you have the most recent versions of the SDK and Eclipse.

Upvotes: 1

Panthro
Panthro

Reputation: 3590

Are your ADT plugin and SDK updated? Mine are updated and doesn't show this error. Works like a charm with just one %.

Anyway, have you tried to put the string inside a CDATA?

<string-array name="details">
     <item>Placement of things</item>
     <item><![CDATA[Percentage of times placed is 30% 
             and percentage of times release 50%]]></item>
</string-array>

Upvotes: 3

Related Questions