Kush
Kush

Reputation: 35

Displaying Superscript and Special symbols in Android

I am using

private final static String MICRO = "&#x3BC";

and

private final static String SQUARE = "&#xB2";

to display ms^2 and MICRO symbol as

case Sensor.TYPE_ACCELEROMETER:
    units = "m/s" + SQUARE;
case Sensor.TYPE_MAGNETIC_FIELD:
    units = MICRO + "T";

But its returning same values. i.e MICRO = "&#x3BC"; and SQUARE = "&#xB2"; on App. Is there an alternative way to display these units?

Upvotes: 1

Views: 1308

Answers (2)

ᴛʜᴇᴘᴀᴛᴇʟ
ᴛʜᴇᴘᴀᴛᴇʟ

Reputation: 4656

Here's another options - use HTML.

First, create XML strings

<string name="ms"><![CDATA[%1$s m/s<sup><small><small>2</small></small><sup>]]></string>
<string name="ut"><![CDATA[%1$s &mu;T]]></string>

Now, use them in your Java:

TextView txtMs = findViewById(R.id.txtMs);
String ms2 = String.format(getResources().getString(R.string.ms), "2.8");
txtMs.setText(Html.fromHtml(ms2));

TextView txtUt = findViewById(R.id.txtUt);
String ut = String.format(getResources().getString(R.string.ut), "2000.0");
txtUt.setText(Html.fromHtml(ut));

Result:

enter image description here

Upvotes: 0

Kush
Kush

Reputation: 35

Just Changing MICRO = "&amp;#x3BC"; to MICRO = "&#x3BC;"; and SQUARE = "&amp;#xB2"; to SQUARE = "&#xB2;"; works

Upvotes: 2

Related Questions