Monday, December 28, 2009

How to quickly add Superscript / Subscript

Hey everyone,

Quick post today. Here’s how you would add superscript/subscript to your text in a TextView.

TextView text = (TextView) findViewById(R.id.text); // retrieve your text view text.setText(Html.fromHtml("5x2"));

And so your text view should now have 5x2 show up.

It’s as simple as that! And if you want subscripts just replace “< sup >” with “< sub >” and you’re good to go.

Finally, depending on what you’re doing, sometimes it appears that the super/sub scripts are “cutoff” by the text view, and so I’ve found that adding a “< small >” clause to the HTML does the trick:

text.setText(Html.fromHtml("5x2"));

Hope this helped some people!

- jwei

PS: I know there is a TextAttribute class in Android/JAVA that you can use, but I just found this way much simpler.

[Via http://thinkandroid.wordpress.com]

No comments:

Post a Comment