1

I'm using the code found: Subscript and Superscript a String in Android in the first answer, but concatenating that from a previous string, so my code looks similar to:

TextView text = (TextView) findViewById(R.id.text);
text.setText(text.getText().toString()+Html.fromHtml("<sup>-2</sup>"));

Say, the contents of text was "3x", after setting the text using setText, it formats to "3x-2" with no subscript.

The XML for the TextView is:

<TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="55dp"
            android:layout_marginTop="210dp"
            android:text="3x"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textSize="14pt"
            android:visibility="VISIBLE" 
/>

Thanks for helping.

Community
  • 1
  • 1
Stefan Dunn
  • 5,363
  • 7
  • 48
  • 84

4 Answers4

7

Instead of this line..

text.setText(text.getText().toString()+Html.fromHtml("<sup>-2</sup>"));

try like this

 text.setText(Html.fromHtml(text.getText().toString()+"<sup>-2</sup>"));
5hssba
  • 8,079
  • 2
  • 33
  • 35
  • Thanks, it worked. The same idea popped into my head just as I was checking Stackoverflow again for replies. – Stefan Dunn Apr 15 '12 at 17:38
  • can i write like this: text.setText("-2"+Html.fromHtml(text.getText().toString())); – nida Dec 11 '13 at 08:56
  • i want textview like this *textviewText while textview text is setting dynamically and i want to put * first as sup script and then textview text – nida Dec 11 '13 at 08:57
4

Please check the below its working fine for me

"TEXT<sup><small>TM</small></sup> TEXT"

The above code will make the TM to Subscript and also make the text size small

sharma_kunal
  • 2,152
  • 1
  • 28
  • 28
2

Also for small size you can use-

text.setText(Html.fromHtml(text.getText().toString()+"<sup><small>-2</small></sup>"));  

Put your whole text into only one Html.fromHtml("Your whole string")

From strings.xml

<string name="superscript_str">YourString&lt;sup&gt;&lt;small&gt;SuperScriptString&lt;/small&gt;&lt;/sup&gt;</string>
darshan patel
  • 736
  • 5
  • 6
0

If you want display small as super text use

text.setText(Html.fromHtml(text.getText().toString()+"<sup>small>-2</small>/sup>"));
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
Raj008
  • 3,539
  • 2
  • 28
  • 26