Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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>

If you want display small as super text use

text.setText(Html.fromHtml(text.getText().toString()+"<sup>small>-2</small>/sup>"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top