Subscript and Superscript a String in Android

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>"