How to quickly add Superscript / Subscript
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("5x<sup>2</sup>"));
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:
Hope this helped some people!
PS: I know there is a TextAttribute class in Android/JAVA that you can use, but I just found this way much simpler.