3

I'm working on app on android studio and i need subscript and superscript in my textview and I know how to do it programmatic-ally like:


  Html.fromHtml(r7.getText().toString()+"  m<sup><small>2</small></sup>")

but when writing it the text of textview give me this error:


Error:(104) Error parsing XML: not well-formed (invalid token)


So how to solve this problem, please ???

BSavaliya
  • 809
  • 1
  • 16
  • 26
hani kamal
  • 55
  • 1
  • 7

2 Answers2

7

please check my answer in this line Simply use SpannableString that will solve your problem.

 SpannableString styledString = new SpannableString("9-10th STD");
 styledString.setSpan(new SuperscriptSpan(), 4, 6, 0);
 textView.setText(styledString);

and put your android:textAllCaps="false"

Community
  • 1
  • 1
AMAN SINGH
  • 3,491
  • 6
  • 28
  • 44
2

According to http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling the only supported HTML markup is <u>, <i>, and <b>. To get sub- and superscripts, you'll probably need to use http://developer.android.com/reference/android/text/style/SubscriptSpan.html and http://developer.android.com/reference/android/text/style/SuperscriptSpan.html .

Sterling
  • 6,365
  • 2
  • 32
  • 40
  • information a bit outdated. Please read carefully https://developer.android.com/guide/topics/resources/string-resource#FormattingAndStyling – Lino Oct 06 '21 at 16:04