1

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.

Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65
user3182266
  • 1,270
  • 4
  • 23
  • 49

3 Answers3

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
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.