Hi I want to ask if it is possible to put/display index character in textview.settext. I want to display CO2 but I want the number 2 to be displayed as an index number (such as in a real chemical formula.
Asked
Active
Viewed 345 times
1
-
Try below code and let me know whether it is working or not. – InnocentKiller Feb 13 '14 at 08:49
3 Answers
4
Hi you can try below code.
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("CO<sup>2</sup>"));
or for subscript you can try it like below.
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("CO<sub>2</sub>"));
Hope this is gonna help you.

InnocentKiller
- 5,234
- 7
- 36
- 84
-
haha this did the trick. Well sort of the number is now high up not down below – user3182266 Feb 13 '14 at 08:52
-
-
ops sorry didn't see the difference between "suP" and "suB" so yeah with suP the number 2 is going UP with suB is going down but if it goes up or down it is "cut" in half by the line size I guess. Is there a way to fix that? – user3182266 Feb 13 '14 at 08:56
-
Use `CO2` instead of `CO2` and it will show you below of the text... And please accept the answer if it helped you. – InnocentKiller Feb 13 '14 at 08:57
-
-
I think your Text-view's height is not set enough that's why it is cutting half, make sure your text-view's height is big and it will show full. – InnocentKiller Feb 13 '14 at 08:58
-
Yep you are right just finished testing it myself everything is fine thank you for your answer :) – user3182266 Feb 13 '14 at 09:04
-
3
Since above answers would be enough for writing the sub index in the formula CO2, they don't answer the generic question , which is displaying special characters.
The problem is that the charset of android fonts do not contain some special characters. This is the Android Character Set
One thing you can try, is to use another Font which contains the characters you want to use. This is the code to load a ttf font and use it in your TextView:
Typeface typeface=Typeface.createFromAsset(getAssets(),"fonts/font_name.ttf");
your_textview.setTypeface(typeface);
Your fonts should be placed in assets/fonts folder (create it if don't exist)

nsL
- 3,722
- 3
- 23
- 40
0
There are supposed to be covered in UTF8 format; I think what you need to look for is Subscript and Superscript.

Sadiq Jaffer
- 72
- 9