7

I want to display on android text view. I tried doing it with unicode but no luck. Please see my code below. Can anyone please help.

additionalInfoString = additionalInfoString.replace("½",<sup><small>&frac12;</small></sup>);
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
  • look at this hope it's help http://stackoverflow.com/questions/2920726/android-displaying-fractions-using-unicode – Farhan Shah Apr 02 '14 at 07:06

5 Answers5

7

You need to use HTML format to show fractions

tv.setText(Html.fromHtml("3<sup>1</sup>/<sub>2</sub>"));

Verify your HTML Text here.

enter image description here

As Html.fromHtml(String s) method has been depreciated. Take a look at this answer SO Answer

SilentKiller
  • 6,944
  • 6
  • 40
  • 75
1

You can use the html formatting of Android TextView. However you must add a extra space on the top and bottom to keep the fraction from being cut off.

  SpannableStringBuilder text = new SpannableStringBuilder();
  text.append("3");
  text.append("\n");
  text.append(Html.fromHtml("<sup>1</sup>/<sub>2</sub>"));
  text.append("\n");

P.S. : The above code is not tested (just a hunch)

Antrromet
  • 15,294
  • 10
  • 60
  • 75
1

you can use unicode character directly as

tv.setText("3\u00BD");

this worked for me.

MohK
  • 1,873
  • 1
  • 18
  • 29
0

Try this:

mTxtVw.setText(Html.fromHtml("3<sup>1</sup>/<sub>2</sub>"));
jyomin
  • 1,957
  • 2
  • 11
  • 27
0

I would suggest you to create two TextView in a RelativeLayout and manage it. because in Html.fromHtml superscript text not comes with proper alignment . use some thing like below

 <RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/maintext_sup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="top"
        android:text="1/2"
        android:textSize="10sp"/>

    <TextView
        android:id="@+id/maintext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:text="3"
        android:textSize="20sp" />
   </RelativeLayout>   
maddy d
  • 1,530
  • 2
  • 12
  • 23