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>
Share:
91,984

Related videos on Youtube

Mohit Deshpande
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, 2020

Comments

  • Mohit Deshpande
    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.

  • Dandre Allison
    Dandre Allison about 12 years
    It technically isn't supporting HTML, that is creating a Spanned, which TextViews do support. Essentially CharSequences with style information.
  • JPM
    JPM about 12 years
    This 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
    Daniel Schuler about 11 years
    This actually looks right, thanks for sharing! The Html.fromHTML() method is convenient, but the superscript isn't smaller.
  • A. Steenbergen
    A. Steenbergen over 9 years
    Isn't parsing this html insanely expensive?
  • m.v.n.kalyani
    m.v.n.kalyani almost 9 years
    i am new to stack over flow ,at that time dnt know how to use.
  • Pang
    Pang over 8 years
    Link in answer doesn't seem relevant anymore.
  • Pang
    Pang over 8 years
    Link in answer doesn't seem relevant anymore.
  • Zach Sperske
    Zach Sperske almost 8 years
    This is way better! The Html.fromHtml method can lead to the superscript text being cut off.
  • Zach Sperske
    Zach Sperske almost 8 years
    Thanks for this but the answer below using a SpannableStringBuilder is much better.
  • Mir-Ismaili
    Mir-Ismaili over 7 years
    fromHtml(String) was deprecated in Android N. Use fromHtml(String, int) instead.
  • Nobody
    Nobody over 7 years
    When 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
    scienticious about 7 years
    Far better than Html.fromHtml() method
  • Aryeetey Solomon Aryeetey
    Aryeetey Solomon Aryeetey almost 5 years
    Please add explanation
  • Kolaaa
    Kolaaa almost 5 years
    Hi,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
    Gerardo Salazar Sánchez almost 5 years
    Hi, yes is unicode, here a PDF whit all unicodes unicode.org/charts/PDF/U2070.pdf
  • Luca Murra
    Luca Murra over 4 years
    According to Mir-Imaili, you can call the same function with Html.fromHtml(String, Html.FROM_HTML_MODE_LEGACY)
  • Izak
    Izak about 4 years
    Thanks. Very simple.