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?

5 Answers 5

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 !

Sign up to request clarification or add additional context in comments.

1 Comment

My superscript tag looks like this: <small><sup><sup><small><small><small>2</small></small></small></small></sup></sup>
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);
}

2 Comments

Thanks for posting that, I'll see if there's a XSpan class for changing the font size in a similar manner as they are doing there.
It's AbsoluteSizeSpan class.
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>"));

Comments

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.

2 Comments

Yeah I have noticed that too, not quite good enough though really.
TagHandler.handleTag is not called for H1's it seems.
-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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.