Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using size HTML attribute in TextView

Tags:

android

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?

like image 620
C0deAttack Avatar asked Aug 30 '11 17:08

C0deAttack


People also ask

How do I Auto Resize TextView?

To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.

Which method is used to change the size of the TextView?

setTextSize(float size) method to set the size of text. textView.

How do I change font size in HTML android?

Go to File -> Settings -> Editor -> Font -> Size.

Which attribute increases or decreases the font size of TextView?

app:autoSizeMinTextSize=”10sp” using this attribute the TextView will be resized up to the size of 10sp and app:autoSizeStepGranularity=”2sp” using this attribute we are uniformly reducing the size of the TextView as 2sp when it goes out of the screen.


2 Answers

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);
}
like image 73
Sergey Glotov Avatar answered Nov 03 '22 18:11

Sergey Glotov


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 !

like image 25
Tobliug Avatar answered Nov 03 '22 16:11

Tobliug