Currently, I am facing a challenge with my webview that is loaded with HTML data. My goal is to dynamically change the font of the HTML content during runtime based on user preferences. To achieve this, I have implemented a feature allowing users to select fonts from the /system/fonts directory.
However, I am struggling to apply the selected font to the webview effectively. I attempted to use JavaScript for this purpose:
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);
}
The above code snippet calls the changeStyle() function 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);
}
'Unfortunately, the implementation is not producing the desired result. While I am confident in the functionality of the changeStyle() JavaScript function – as it successfully modifies font size and color – it fails to update the font as intended.