Android show HTML text as bold with custom font
android, android-fonts
Solution
Use:
body.setTypeface(tf, Typeface.BOLD);
Typeface
Also you can set typeface on webView. Just add something like this:
<html> <head><style type=\"text/css\"> @font-face { font-family: MyFont; src: url(\"file:///android_asset/font.ttf\") } body { font-family: MyFont; font-size: 12px; text-align: justify; } </style> </head><body>
and load using:
webView.loadDataWithBaseURL("file:///android_asset/", result, "text/html", "UTF-8", "null");
//FOR EDITED QUESTION
String s = getResources().getString(R.string.string1);
((TextView) findViewById(R.id.t)).setText(Html.fromHtml(s), TextView.BufferType.SPANNABLE);
Problem
I'm having a little trouble getting my custom font to show up bold, here's what I'm trying to do: I've set an HTML formatted string in res/string ``` <string name="string1"> <![CDATA[<b>Title1</b><p>sub text</p>]]> </string> ``` Then I set the font at runtime (where getString gets a resID from a string): ``` Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/eurostileltstddemi.ttf"); TextView body = (TextView) findViewById(R.id.tv_body); body.setText(Html.fromHtml(getString(name))); body.setTypeface(tf); ``` Problem being that the text doesn't show up as bold where it should, any ideas where I'm going wrong?? Sorry forgot to add that I need some text as bold and some not.