0

I recover chemical formula from a json file, and to write them in the good way (with number in subscript), I use this method:

String s = item.get(NAME);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
    if (s.charAt(i) == '2' || s.charAt(i) == '3' || s.charAt(i) == '4'
        || s.charAt(i) == '5' || s.charAt(i) == '6') {
    sb.append("<sub>").append(s.charAt(i)).append("</sub>");
    } else {
    sb.append(s.charAt(i));
    }
}

name.setText(Html.fromHtml(sb.toString()));

It works fine but the font size of the subscript letters is the same than those of the others. And I'd like it is smaller. So is there a way to change the size of specific letters in a word ?

Thanks

Turvy
  • 882
  • 1
  • 8
  • 23

3 Answers3

3

You can use an AbsoluteSizeSpan to do this. E.g.:

Spannable span = new SpannableString(text);
span.setSpan(new AbsoluteSizeSpan(14, true), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(span, TextView.BufferType.SPANNABLE);

start and end define the range of characters to change.

Ryan Joseph
  • 105
  • 6
2

You can use <font size="number"><font>, where number means the size of the text you want. The code will work in strings, I've tried it.

Andrei
  • 330
  • 1
  • 3
  • 12
0

According to android.text.Html (on GrepCode), you may use <small> for smaller text, as <font> only supports color and face attributes.

for example H2O

String s = "<b>H</b><small><sub>2</sub></small><b>O</b>";
MilapTank
  • 9,988
  • 7
  • 38
  • 53
  • Thanks, just that I wanted. Thus I simply change 'sb.append("").append(s.charAt(i)).append("");' to ' sb.append("").append(s.charAt(i)) .append("");' – Turvy Sep 16 '14 at 13:42
  • Hey try my updated Answer so its look more batter :) happy to help you – MilapTank Sep 16 '14 at 13:45
  • Sorry, but I don't like it :). Even if they have to be smaller, numbers have also to be visible. And on a phone, it will be difficult with capital letters in bold. – Turvy Sep 16 '14 at 14:24