user4914918
user4914918

Reputation: 91

How do I write a fraction in android studio

How to do I write a fraction in android studio. What's the best practice for a fraction with a horizontal line I am trying to produce.

Upvotes: 5

Views: 3508

Answers (3)

Mou
Mou

Reputation: 2147

You can try this solution: http://blog.sqisland.com/2014/11/android-stacked-fractions.html

Basically, you have to looking for a font that supports afrc. Now, you can play with your TextViews and TagHandlers until you get the result desired.

Upvotes: 2

penta
penta

Reputation: 2596

The solution does not seem simple, however if you want to try fraction in xml, go to this website.

http://unicode-search.net/unicode-namesearch.pl?term=fraction

This code worked for me to show 14.

The xml code is.

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="\u00BC"
            android:id="@+id/textView"
            android:textSize="25sp" />

The required line is android:text="\u00BC" to show 14 as fraction.

So if you want to show 12 or any other as fraction from the above mentioned website, just append the last two characters(in this case BD) to \u00.

So 1/2 becomes \u00BD

Upvotes: 2

Zahan Safallwa
Zahan Safallwa

Reputation: 3914

Use html format. However you must add a extra space on the top and bottom to keep it from being cut off.

 SpannableStringBuilder test = new SpannableStringBuilder();
 test.append("\n");
 test.append(Html.fromHtml("<sup>5</sup>/<sub>9</sub>"));
 test.append("\n");

Upvotes: 3

Related Questions