1

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

Abdul Hoque Nuri
  • 1,105
  • 1
  • 9
  • 18
judy
  • 325
  • 3
  • 15

3 Answers3

1

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>
Saurabh Vadhva
  • 511
  • 5
  • 12
1

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

Nitish
  • 3,075
  • 3
  • 13
  • 28
  • Great. thank you. I am wondering why Android studio suggested me wrong unicodes . but this one works. thanks – judy Oct 07 '21 at 14:01
1

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

Milan.Tejani
  • 162
  • 9
  • [Html.fromHtml is deprecated](https://stackoverflow.com/questions/37904739/html-fromhtml-deprecated-in-android-n) , consider switching to the latest one – Nitish Oct 07 '21 at 05:35