This works great for me on all the devices I've tested it on.
Place your font files in /src/main/assets/fonts, then use this method:
public class HTMLUtils {
public static String getLocalFontInHTML(String html, String fontFamily, String fontFileName) {
return "<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
"<style>" +
"@font-face {" +
"font-family: " + fontFamily + ";" +
"src: url('" + fontFileName + "');" +
"}" +
"* {font-family: '" + fontFamily + "' !important;}" +
"* {font-size: 1rem !important;}" +
"</style>" +
"</head>\n" +
"<body>\n" +
html +
"\n" +
"</body>\n" +
"</html>";
}
}
as follows
webView.loadDataWithBaseURL("file:///android_asset/", HTMLUtils.getLocalFontInHTML(item.text, Config.FONT_FAMILY, Config.REGULAR_FONT),"text/html", "UTF-8", null);
where
public static final String FONT_FAMILY = "Montserrat";
public static final String REGULAR_FONT = "fonts/Montserrat-Regular.otf";
Hope it helps someone!
If you want to keep the font size from your html code remove
"* {font-size: 1rem !important;}"
Have in mind that 1rem is equivalent to about 14sp