So I\'m trying to add superscipt/subscript to a TextView, basically trying to make a fraction be displayed as 1/2. Not sure where I\'m going wrong, and
You can use RelativeSizeSpan
numSpan.setSpan(new SuperscriptSpan(), 0, numSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
numSpan.setSpan(new RelativeSizeSpan(0.5f), 0, numSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
or AbsoluteSizeSpan
numSpan.setSpan(new SuperscriptSpan(), 0, numSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
numSpan.setSpan(new AbsoluteSizeSpan(28), 0, numSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Try Html.fromHtml
instead. Simple and precise:
outputText.setText(Html.fromHtml("<sup>" + num + "</sup>/<sub>" + den + "</sub>"));
Note: You may need to give some height to the textview else the output will be clipped.
Alternatively, you could try this, where the number size is smaller:
outputText.setText(Html.fromHtml("<sup><small>" + num + "</small></sup>/<sub><small>" + den + "</small></sub>"));