Subscript and Superscript a String in Android
91,984
Solution 1
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));
or
Common Tasks and How to Do Them in Android
Solution 2
Example:
equation = (TextView) findViewById(R.id.textView1);
SpannableStringBuilder cs = new SpannableStringBuilder("X3 + X2");
cs.setSpan(new SuperscriptSpan(), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
cs.setSpan(new RelativeSizeSpan(0.75f), 1, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
cs.setSpan(new SuperscriptSpan(), 6, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
cs.setSpan(new RelativeSizeSpan(0.75f), 6, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
equation.setText(cs);
Solution 3
To all people asking, if you want to make it smaller besides of making super or subscript, you just need to add tag as well. EX:
"X <sup><small> 2 </small></sup>"
Solution 4
In the code just put this "\u00B2" Like this:
textView.setText("X\u00B2");
Solution 5
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup><small>2</small></sup>"));
(or) From String Resource File:
<string name="test_string">
<![CDATA[ X<sup><small>2</small></sup> ]]>
</string>
Related videos on Youtube
Author by
Mohit Deshpande
I am a researcher at The Ohio State University in the field of computer vision and machine learning. I have worked as a mobile apps instructor for Zenva and current work as a writer for Zenva in machine learning.
Updated on November 06, 2020Comments
-
Mohit Deshpande over 3 years
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.-
Omid Omidi over 10 yearsuse [this][1] trick in listview and its textview. [1]: stackoverflow.com/a/22105902/1350021
-
-
Dandre Allison about 12 yearsIt technically isn't supporting HTML, that is creating a Spanned, which TextViews do support. Essentially CharSequences with style information.
-
JPM about 12 yearsThis doesn't work for me...but maybe its cause I set it inside my strings.xml file. It subscripts it for me but it clips it and no matter how much padding I put its always clipped.
-
Daniel Schuler about 11 yearsThis actually looks right, thanks for sharing! The Html.fromHTML() method is convenient, but the superscript isn't smaller.
-
A. Steenbergen over 9 yearsIsn't parsing this html insanely expensive?
-
m.v.n.kalyani almost 9 yearsi am new to stack over flow ,at that time dnt know how to use.
-
Pang over 8 yearsLink in answer doesn't seem relevant anymore.
-
Pang over 8 yearsLink in answer doesn't seem relevant anymore.
-
Zach Sperske almost 8 yearsThis is way better! The Html.fromHtml method can lead to the superscript text being cut off.
-
Zach Sperske almost 8 yearsThanks for this but the answer below using a SpannableStringBuilder is much better.
-
Mir-Ismaili over 7 years
fromHtml(String)
was deprecated in Android N. UsefromHtml(String, int)
instead. -
Nobody over 7 yearsWhen I use this method like to say 100 meter square by
equation.setText(blah+cs);
it doesn't work. Works fine separately though. How to get that work? -
scienticious about 7 yearsFar better than Html.fromHtml() method
-
Aryeetey Solomon Aryeetey almost 5 yearsPlease add explanation
-
Kolaaa almost 5 yearsHi,your answer was very helpful. However, what code can be used for subscript ? Also what are these codes called if i what to google them, are they unicodes ?
-
Gerardo Salazar Sánchez almost 5 yearsHi, yes is unicode, here a PDF whit all unicodes unicode.org/charts/PDF/U2070.pdf
-
Luca Murra over 4 yearsAccording to Mir-Imaili, you can call the same function with Html.fromHtml(String, Html.FROM_HTML_MODE_LEGACY)
-
Izak about 4 yearsThanks. Very simple.