Home > Back-end >  Storing Fractional character in string values android
Storing Fractional character in string values android

Time:10-07

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

CodePudding user response:

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>

CodePudding user response:

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>

enter image description here

CodePudding user response:

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

  • Related