How to Change Font Face of Webview in Android

Change Font WebView AndroidStudio

The ttf-file is in the same folder as the .java

That is pointless for two reasons:

  • Android doesn't typically use random files alongside the Java classes

  • WebView doesn't know about any of that, anyway

Create an src/main/assets/ directory (assuming that you are working in a typical Android Studio project). Put your font files there. Use loadDataWithBaseURL(), using file:///android_asset/ as the URL. Then, hope for the best.

Custom font for webview

There is a bug report for Android that seems to suggest that custom fonts don't work for certain languages (Thai, Hebrew, Farsi, and Arabic have been mentioned). This hasn't been confirmed by anyone from Google, but there are a number of people reporting the same problem.

One of the suggested solutions (which at least one user confirmed as working) was to convert the font from otf to svg. There are a number of online tools that will do this for you (Everything Fonts is one example), but you may have issues with this being a copyrighted font - make sure your font license permits such conversions.

Also, when you have converted the font, open it up in a text editor and check whether the svg element is namespaced. It should look something like this:

<svg xmlns="http://www.w3.org/2000/svg">

If it is just <svg> without the namespace, you may need to add the xmlns attribute manually. Apparently, svg fonts without the namespace don't work either.



Related Topics



Leave a reply



Submit