user3051755 user3051755 - 5 months ago 19
Android Question

Different typeface for parts of text

I have two typefaces and I would like to use Typeface1 for the titles and Typeface2 for the body.

Is there a way to do that? I can add tags around the title if that would help, like

<b>Title</b>


Typeface typeface1 = Typeface.createFromAsset(getAssets(), "fonts/Typeface1.ttf");
Typeface typeface2 = Typeface.createFromAsset(getAssets(), "fonts/Typeface2.ttf");
textView.setTypeface(typeface1);

textView.setText("<b>Title 1</b>\n" +
"Body 1\n" +
"<b>Title 2</b>\n" +
"Body 2\n" +
"<b>Title 3</b>\n" +
"Body 3");

Answer

You need to use Html.fromHtml() to use HTML in your XML Strings. Simply referencing a String with HTML in your layout XML will not work.

Example:

textView.setText(Html.fromHtml("<b>Title</b><br>Body1<br><b>Title2</b><br>Body2"));

This might work

Comments