views:

310

answers:

4

How can you print a string with a subscript or superscript? Can you do this without an external library? I want this to display in a TextView in Android.

A: 

If you mean printing a String in Swing or AWT, then take a look at the TextAttribute class, which is used in conjunction with the Font class. It has a SUPERSCRIPT Integer attribute.

I think you'll need to give a little more detail as to where exactly you're printing this String.

Noel M
I clarified that I want this in a TextView in Android.
Mohit Deshpande
A: 

For android, I'm not sure there's a way to do this in a TextView. You may want to use a webview instead so you can use html.

Falmarri
+1  A: 

I found this article on how to use a Spannable or in a string resource file: <sup> or <sub> for superscript and subscript, respectively.

Mohit Deshpande
+7  A: 
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));

or

http://developer.android.com/intl/zh-TW/guide/appendix/faq/commontasks.html#selectingtext

Konstantin Burov
+1, didn't know textview supported html
Falmarri