[Android] Apply Custom Font to WebView

First I greatly suggest this Android library named as Calligraphy. Thanks to its author. This helps a lot to use custom font in the Android app. But using this library would not set the custom font in WebView.

So back to our solution, first place the font TTF file as “app/assets/fonts/xxx.ttf”.

Second, when customizing the WebView, applying the HTML content with CSS here.

String header = "<html><head><style type="text/css">@font-face {font-family: MyFont;src: url("file:///android_asset/fonts/xxx.ttf")} body {font-family: MyFont;font-size: medium;text-align: justify;}</style></head><body>";

String footer = "</body></html>";

String htmlString = header + Html.fromHtml(description) + footer;

webView.loadDataWithBaseURL(null, htmlString, "text/html", "UTF-8", null);