2

I have this code for setting the text of a TextView:

TextView txt = new TextView(this);
txt.setText(Html.fromHtml("<b>" + m.getTitle() + "</b>" +  "<br />" + "<small>" + m.getText() + "</small>" + "<br />");

The <small> mark is working, but I'd like to set the text size according to my dimensions defined in the dimens.xml file, which I use for all other text in my application. Adding the TextView through an xml layout is not an option since I don't know how many TextViews I'll be adding.

Dimensions in the dimens.xml file are set up like <dimen name="text_size_320dp_small">16sp</dimen>.

How can I apply these dimensions to my text formatted with Html.fromHtml?

Thanks a lot.

Blueriver
  • 3,212
  • 3
  • 16
  • 33
  • Why are you using HTML for this in the first place? Use two `TextView` widgets in a vertical `LinearLayout`. Set the `textSize` on each `TextView`, and set the `textStyle` on the first one to be `bold`. Done. If you want, create your own `TitleAndTextView` that extends `LinearLayout` and wraps all this stuff up in a single compound widget. – CommonsWare Jul 18 '15 at 19:37

4 Answers4

3

I have tested following code myself. You can do it like this.

txt.setText(Html.fromHtml("<b>" + m.getTitle() + "</b>" + "<br />" + "<font textsize=" + getResources().getDimension(R.dimen.text_size_320dp_small) + ">" + m.getText() + "</font>" + "<br />"));

Bunny
  • 576
  • 4
  • 19
  • The code is not changing text size for me when I change the values in text_size_320dp_small (I have tested changing from 16sp to 26sp). From what I've read in other questions here in SO the font tag is ignored by `Html.fromHtml`. Could you please tell me if it's actually changing text size for you, and if it is, what SDK version are you targeting, or if you're using regular TextView, or any other difference our codes might have? I can provide you with my full code, but I think your snippet is shorter, and it should be my job to find the differences. Thank you. – Blueriver Jul 18 '15 at 06:26
  • @Bunhy: Please note that the `textsize` attribute shows up nowhere in [the source code to the `Html` class](https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/text/Html.java). – CommonsWare Jul 18 '15 at 19:36
2

[Updated]:

Just came up with some references and updates :

You can store this in strings.xml

<string name="mystring">&lt;font size = &quot;%s&quot;&gt;</string>

In code you can write as:

int sptopx = getResources().getDimensionPixelSize(R.dimen.text_size_320dp_small);
Spanned modified = Html.fromHtml( context.getString(R.string.mystring, sptopx) );
myTextView.setText(spanned);

TextView txt = new TextView(this);

        txt.setText(
        Html.fromHtml(
        "<b>" + m.getTitle() + "</b>" + 
        "<br />" + 
        modified + 
            ">" + m.getText() + "</font>" + 
            "<br />"
            )
            );

for details about html tags support in TextViews you can check this link.

  • Sadly it didn't. `Html.fromHtml` ignores the font tag, so size is not applied that way. Still, I appreciate your time and effort. – Blueriver Jul 19 '15 at 00:27
1

Why don't you use txt.setSizeText(yoursize)? However you can retrieve your dimensions using this:

float yourDimen = getResources().getDimension(R.dimen.your_dimen_name);
  • I don't use that because that sets size for the whole text, instead of just part of it. I will still consider using your alternative with multiple textviews, though for now it looks quite impractical. Thank you for your answer! – Blueriver Jul 18 '15 at 06:04
1

You can't directly, the small tag creates a RelativeSizeSpan with a proportion of .8f, which is hardcoded into the implementation of Html.fromHtml.

Leaves two options that I can see, set the text size to 20sp (which would make small work out to 16sp). Probably not ideal.

The other option is to use a custom tag <mySmall> by replacing all occurrences of <small> and </small> with <mySmall>& </mySmall>. And then call fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler) with a TagHandler that integrates a AbsoluteSizeSpan into the output Editable.

pturner
  • 686
  • 5
  • 14
  • I... ehm... wow. I'm having trouble implementing the `Html.TagHandler`. I haven't taken the necessary time to fully explore this solution, but I did try this post's accepted answer: http://stackoverflow.com/questions/4044509/android-how-to-use-the-html-taghandler and can't really understand where to use the `AbsoluteSizeSpan` and how. I'll keep working on it, but if you could please provide a snippet or some pointers I'd be even more grateful than I already am. – Blueriver Jul 18 '15 at 08:12
  • So, I could successfully implement this. Now it's a matter of detecting the screen width programmatically and adding the right tags according to it, but my TagHandler handles tags and gets the dimens.xml file. Thank you! A lot!! Quick question though: since i – Blueriver Jul 19 '15 at 00:25
  • Looks like your comment cut off yesterday, anything I can help? – pturner Jul 20 '15 at 14:15