Search code examples
androidtextviewsuperscript

TextView Superscript not showing correctly


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.


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