judy
judy

Reputation: 335

Storing Fractional character in string values android

When storing fractional numbers eg 1/2 in strings.xml:

<string-array name="array_fractionals">
    <item>0</item>
    <item>1/2</item>
     <item>1/5</item>
    <item>1/8</item>
</string-array>

It get a suggestion to use it like:

    <item><![CDATA[&#8531;]]></item>
    <item><![CDATA[&#188;]]></item>

And when I take suggestion retrive values:

String[] array_frac=getResources().getStringArray(R.array.array_fractionals);
            numberPickerTwo.setDisplayedValues(array_frac);

It won't print "1/2" but will show the below: what is the mechanism required to decode that to fractional character?

enter image description here

Upvotes: 1

Views: 95

Answers (3)

Milan.Tejani
Milan.Tejani

Reputation: 162

you can also do this:-

Spanned[] spanneds = new Spanned[]{
            Html.fromHtml("<sup>1</sup>/<sub>2</sub>"),
            Html.fromHtml("<sup>1</sup>/<sub>5</sub>"),
            Html.fromHtml("<sup>1</sup>/<sub>8</sub>")
    };

and use this array in text

yourtextview.setText(spanneds[1]);

which give you this output :-

enter image description here

Upvotes: 1

Nitish
Nitish

Reputation: 3411

You are using the wrong unicode/escape code values.
Update your unicode values to following:

<string-array name="array_fractionals">
    <item>0</item>
    <item><![CDATA[\u00BD]]></item>   // values for 1/2
    <item><![CDATA[\u2155]]></item>  // values for 1/5
</string-array>

Unicode values - refer this link to find all required unicode values (look for c, java unicode values)

Final Result:

enter image description here

Upvotes: 1

Saurabh Vadhva
Saurabh Vadhva

Reputation: 511

Please try this

<string-array name="array_fractionals">
    <item>0</item>
    <item tools:ignore="TypographyFractions">1/2</item>
    <item>1/5</item>
    <item>1/8</item>
</string-array>

Upvotes: 1

Related Questions