Android subscript and superscript

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

+81
java android string subscript superscript
Aug 22 '10 at 21:21
source share
12 answers
 ((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>")); 

or

Common tasks and how to do them in Android

+128
Aug 22 2018-10-22T00:
source share

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); 
+89
Jan 15 '12 at 20:13
source share

For all people asking if you want to do this less than creating a super or index, you just need to add a tag. EX:

 "X <sup><small> 2 </small></sup>" 
+38
May 3 '13 at 3:27
source share

If you want to set the superscript from the string.xml file, try the following:

line resource:

 <string name="test_string">X&lt;sup&gt;3&lt;/sup&gt;</string> 

if you want superscript to be less:

 <string name="test_string">X&lt;sup&gt;&lt;small&gt;3&lt;/small&gt;&lt;/sup&gt;</string> 

The code:

 textView.setText(Html.fromHtml("Anything you want to put here"+getString(R.string.test_string))); 
+7
Sep 03 '14 at 20:35
source share

This is a bit late, but after just working fine, use the superscript as a special character, here I used a spatial char.

 <string name="str">Hβ‚‚</string> 
+7
May 12 '15 at 9:52
source share
 ((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup><small>2</small></sup>")); 

(or) from the line resource file:

 <string name="test_string"> <![CDATA[ X<sup><small>2</small></sup> ]]> </string> 
+6
Apr 04 '15 at 9:45
source share

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

+3
Aug 22 '10 at 22:01
source share

HTML.fromHTML (String) has been deprecated since API 24. They say that instead they use this one that supports flags as a parameter. So, to discard 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 need code that also considers APIs up to 24:

 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 sourced from: https://stackoverflow.com/a/167189/

Flags and other documentation can be found here: https://developer.android.com/reference/android/text/Html.html

+2
Oct 17 '16 at 5:21
source share

The accepted answer is out of date. So please go through this piece of code. I got this from some website. I forgot the name, but thanks anyway for this nice 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" your spantext.

+1
Feb 08 '17 at 11:59 on
source share

They are called Unicode characters, and Android TextView supports them. Copy super / under script from this wiki: https://en.wikipedia.org/wiki/List_of_Unicode_characters#Superscripts_and_Subscripts

0
Sep 04 '15 at 13:15
source share

In strings.xml files strings.xml you can simply use the HTML <sup>3</sup> . Work great for me

Example

 <string name="turnoverRate">Turnover rate m<sup>3</sup>/m<sup>2</sup>/hour:</string> 
-one
Jul 22 '15 at 7:54
source share
 yourTextView.setText(Html.fromHtml("X<sup>2</sup>")); This will be the result in you yourTextView = 

X 2

-one
Feb 23 '16 at 4:01
source share



All Articles