Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display fractions in android textview?

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>);
like image 957
androidDeveloer Avatar asked Apr 02 '14 07:04

androidDeveloer


2 Answers

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

like image 57
SilentKiller Avatar answered Sep 18 '22 18:09

SilentKiller


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)

like image 44
Antrromet Avatar answered Sep 20 '22 18:09

Antrromet