Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 439
JPM Avatar asked Mar 28 '11 21:03

JPM


4 Answers

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>
like image 185
Panthro Avatar answered Nov 17 '22 00:11

Panthro


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.

like image 23
JPM Avatar answered Nov 16 '22 23:11

JPM


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

like image 1
j.c Avatar answered Nov 17 '22 01:11

j.c


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

like image 1
Bao Le Avatar answered Nov 17 '22 00:11

Bao Le