12

I have the following:

textView.setText(Html.fromHtml("<font color=\"red\" size=\"24\">Hello</font>"));

The string 'Hello' does turn red but the size does not change.

It is as if the size attribute is just ignored, does anyone know why this is? Am I doing something wrong?

Sergey Glotov
  • 19,939
  • 11
  • 82
  • 96
C0deAttack
  • 24,123
  • 18
  • 70
  • 81

5 Answers5

16

Size attribute seems not working.

You can use <small> or <big> (multiple times to increase the effect)

You can also use <h1> to <h6> (Header only, i.e. add a new line)

Its old-fashion, but it works well !

Tobliug
  • 2,852
  • 28
  • 28
11

Yes, size attribute just ignored. Only "color" and "face" attributes are taken into account.

From Html class sources:

private void handleStartTag(String tag, Attributes attributes) {
    if (tag.equalsIgnoreCase("br")) {
        // We don't need to handle this. TagSoup will ensure that there's a </br> for each <br>
        // so we can safely emite the linebreaks when we handle the close tag.
    }
    ...
    else if (tag.equalsIgnoreCase("font")) {
        startFont(mSpannableStringBuilder, attributes);
    }
    ...
}

private static void startFont(SpannableStringBuilder text,
                              Attributes attributes) {
    String color = attributes.getValue("", "color");
    String face = attributes.getValue("", "face");

    int len = text.length();
    text.setSpan(new Font(color, face), len, len, Spannable.SPAN_MARK_MARK);
}
Sergey Glotov
  • 19,939
  • 11
  • 82
  • 96
3

Try this one,Its working for me,use small,big key words

TextView mBox = (TextView) findViewById(R.id.txt);
mBox.setText(Html.fromHtml("<font color=#cc0029>" + "<b>"
        + "Hiiiiiiiiii" + "</b>" + "<br />" + "<small>" + "description"
        + "</small>" + "<br />" + "<small>" + "DateAdded" + "</small>"));
madhu527
  • 4,534
  • 1
  • 27
  • 28
1

Sergey Gotov is right. The only way to change text size it to use h1 - h6 tags.

EDIT: You can also implement TagHandler and use your own tags.

Michael
  • 52,489
  • 21
  • 132
  • 138
-2

Look at Formatting and Styling on the android developpers site:
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling

Or, on this old post of StackOverflow :
Highlighting Text Color using Html.fromHtml() in Android?

Community
  • 1
  • 1
Yann Masoch
  • 1,598
  • 1
  • 12
  • 20