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.

- 68,980
- 16
- 115
- 93

- 53,877
- 76
- 193
- 251
-
use [this][1] trick in listview and its textview. [1]: http://stackoverflow.com/a/22105902/1350021 – Omid Omidi Feb 28 '14 at 21:16
15 Answers
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));
or

- 6,579
- 7
- 67
- 92

- 68,980
- 16
- 115
- 93
-
4It technically isn't supporting HTML, that is creating a Spanned, which TextViews do support. Essentially CharSequences with style information. – Dandre Allison Apr 20 '12 at 17:21
-
1This 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. – JPM Jun 01 '12 at 17:22
-
-
15
-
4Thanks for this but the answer below using a SpannableStringBuilder is much better. – Zach Sperske Aug 12 '16 at 21:21
-
`fromHtml(String)` was deprecated in Android N. [Use `fromHtml(String, int)` instead](http://stackoverflow.com/a/37905107/5318303). – Mir-Ismaili Jan 03 '17 at 11:57
-
1According to Mir-Imaili, you can call the same function with Html.fromHtml(String, Html.FROM_HTML_MODE_LEGACY) – Luca Murra Nov 09 '19 at 15:23
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);
-
2This actually looks right, thanks for sharing! The Html.fromHTML() method is convenient, but the superscript isn't smaller. – Daniel Schuler Apr 20 '13 at 00:30
-
This is way better! The Html.fromHtml method can lead to the superscript text being cut off. – Zach Sperske Aug 12 '16 at 21:20
-
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? – Nobody Mar 25 '17 at 14:35
-
-
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>"

- 775
- 1
- 6
- 15
In the code just put this "\u00B2" Like this:
textView.setText("X\u00B2");

- 381
- 2
- 9
-
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 ? – Kolaaa Aug 10 '19 at 17:22
-
Hi, yes is unicode, here a PDF whit all unicodes https://www.unicode.org/charts/PDF/U2070.pdf – Gerardo Salazar Sánchez Aug 30 '19 at 22:12
It bit late but following just work fine, use superscript as special character, I used spacial char here.
<string name="str">H₂</string>

- 11,056
- 14
- 90
- 197
((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>

- 6,598
- 3
- 37
- 43

- 760
- 7
- 20
The Accepted answer is deprecated now. So please go through this piece of code. I got this from some website. I forgot the name but anyway thanks for this good piece of working code.
SpannableString styledString
= new SpannableString("Large\n\n" // index 0 - 5
+ "Bold\n\n" // index 7 - 11
+ "Underlined\n\n" // index 13 - 23
+ "Italic\n\n" // index 25 - 31
+ "Strikethrough\n\n" // index 33 - 46
+ "Colored\n\n" // index 48 - 55
+ "Highlighted\n\n" // index 57 - 68
+ "K Superscript\n\n" // "Superscript" index 72 - 83
+ "K Subscript\n\n" // "Subscript" index 87 - 96
+ "Url\n\n" // index 98 - 101
+ "Clickable\n\n"); // index 103 - 112
// make the text twice as large
styledString.setSpan(new RelativeSizeSpan(2f), 0, 5, 0);
// make text bold
styledString.setSpan(new StyleSpan(Typeface.BOLD), 7, 11, 0);
// underline text
styledString.setSpan(new UnderlineSpan(), 13, 23, 0);
// make text italic
styledString.setSpan(new StyleSpan(Typeface.ITALIC), 25, 31, 0);
styledString.setSpan(new StrikethroughSpan(), 33, 46, 0);
// change text color
styledString.setSpan(new ForegroundColorSpan(Color.GREEN), 48, 55, 0);
// highlight text
styledString.setSpan(new BackgroundColorSpan(Color.CYAN), 57, 68, 0);
// superscript
styledString.setSpan(new SuperscriptSpan(), 72, 83, 0);
// make the superscript text smaller
styledString.setSpan(new RelativeSizeSpan(0.5f), 72, 83, 0);
// subscript
styledString.setSpan(new SubscriptSpan(), 87, 96, 0);
// make the subscript text smaller
styledString.setSpan(new RelativeSizeSpan(0.5f), 87, 96, 0);
// url
styledString.setSpan(new URLSpan("http://www.google.com"), 98, 101, 0);
// clickable text
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
// We display a Toast. You could do anything you want here.
Toast.makeText(MainActivity.this, "Clicked", Toast.LENGTH_SHORT).show();
}
};
styledString.setSpan(clickableSpan, 103, 112, 0);
// Give the styled string to a TextView
spantext = (TextView) findViewById(R.id.spantext);
// this step is mandated for the url and clickable styles.
spantext.setMovementMethod(LinkMovementMethod.getInstance());
// make it neat
spantext.setGravity(Gravity.CENTER);
spantext.setBackgroundColor(Color.WHITE);
spantext.setText(styledString);
Note : Always put android:textAllCaps="false"
of your spantext.

- 3,491
- 6
- 28
- 44
If you want to set the superscript from string.xml file try this:
string resource:
<string name="test_string">X<sup>3</sup></string>
if you want the superscript to be smaller:
<string name="test_string">X<sup><small>3</small></sup></string>
Code:
textView.setText(Html.fromHtml("Anything you want to put here"+getString(R.string.test_string)));

- 598
- 5
- 9
Android String Resource Superscript and Subscript for letters
You don't really have to use html document if any of the letters you want is represented here
For "a" copy and paste this "ᵃ"
You can copy and paste any of these Superscripts and Subscripts directly into your Android String Resource.
Example:
<string name="word_with_superscript" translatable="false">Trademark ᵀᴹ</string>
Result:Trademark ᵀᴹ
Superscript and Subscript letters
Superscript capital ᴬ ᴮ ᴰ ᴱ ᴳ ᴴ ᴵ ᴶ ᴷ ᴸ ᴹ ᴺ ᴼ ᴾ ᴿ ᵀ ᵁ ⱽ ᵂ
Superscript minuscule ᵃ ᵇ ᶜ ᵈ ᵉ ᶠ ᵍ ʰ ⁱ ʲ ᵏ ˡ ᵐ ⁿ ᵒ ᵖ ʳ ˢ ᵗ ᵘ ᵛ ʷ ˣ ʸ ᶻ
Subscript minuscule ₐ ₑ ₕ ᵢ ⱼ ₖ ₗ ₘ ₙ ₒ ₚ ᵣ ₛ ₜ ᵤ ᵥ ₓ

- 1
- 1

- 61
- 1
- 2
The HTML.fromHTML(String) was deprecated as of API 24. They say to use this one instead, which supports flags as a parameter. So to go off of the accepted answer:
TextView textView = ((TextView)findViewById(R.id.text));
textView.setText(Html.fromHtml("X<sup>2</sup>", Html.FROM_HTML_MODE_LEGACY));
And if you want code that considers pre-24 API's as well:
TextView textView = ((TextView)findViewById(R.id.text));
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml("X<sup>2</sup>", Html.FROM_HTML_MODE_LEGACY));
} else {
textView.setText(Html.fromHtml("X<sup>2</sup>"));
}
This answer was derived from: https://stackoverflow.com/a/37905107/4998704
The flags and other documentation can be found here: https://developer.android.com/reference/android/text/Html.html

- 1
- 1

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

- 53,877
- 76
- 193
- 251
In the strings.xml
files, you can just use the HTML <sup>3</sup>
tag. Work perfectly for me
EXAMPLE
<string name="turnoverRate">Turnover rate m<sup>3</sup>/m<sup>2</sup>/hour:</string>

- 2,868
- 9
- 30
- 65
They are called Unicode characters, and Android TextView
supports them. Copy the super/sub-script you want from this Wiki: https://en.wikipedia.org/wiki/List_of_Unicode_characters#Superscripts_and_Subscripts

- 1,120
- 13
- 25
yourTextView.setText(Html.fromHtml("X<sup>2</sup>"));
This will be the result in you yourTextView =
X2

- 119
- 1
- 5