Saturday, September 14, 2013

How to define a different bold font in Android textview

How to define a different bold font in Android textview

I need to use a different font for the bold elements in HTML text rendered
in an Android textview.
I tried doing it this way, but it looks like the Typeface style argument
just sets the entire textview to normal or bold, rather than setting the
font for that style in the textview.
TextView tv = (TextView) dialogView.findViewById(R.id.textInstructions);
String instructions = "<b>Headline</b><br/>Regular Text";
tv.setText(Html.fromHtml(instructions));
Typeface font = Typeface.createFromAsset(BaseActivity.this.getAssets(),
"OpenSans-Regular.ttf");
Typeface bold = Typeface.createFromAsset(BaseActivity.this.getAssets(),
"OpenSans-Bold.ttf");
tv.setTypeface(font, Typeface.NORMAL);
tv.setTypeface(bold, Typeface.BOLD);

No comments:

Post a Comment