I have a webview loaded with HTML data. I want to be able to change the font of the HTML at runtime. For this I have a setting where the user can choose a font (the fonts are taken from the /system/fonts directory).
How do I apply the selected font to the webview?
I tried this by using javascript:
String fontString = "font-family: 'myFont'; url: /system/fonts/DroidSans.ttf;";
if (android.os.Build.VERSION.SDK_INT < 19) {
webview.loadUrl("javascript:changeStyle('body', '" + fontString + "')");
} else {
webview.evaluateJavascript("javascript:changeStyle('body', '" + fontString + "')", null);
}
With changeStyle() defined in the HTML's javascript:
function changeStyle(tag, style) {
var myList = document.getElementsByTagName(tag); // get all p elements
var x = myList.length;
myList[0].setAttribute("style", style);
}
This is, however, not working. I'm pretty sure there is nothing wrong with the changeStyle() js-function, for it is working to set the font size or color.