Skip to content

Instantly share code, notes, and snippets.

@praveenb
Forked from andrea-ale-sbarra/Custom font in WebView
Last active September 26, 2020 20:13
Show Gist options
  • Select an option

  • Save praveenb/3b8e6af14c26b5211d9d30adaebd83b1 to your computer and use it in GitHub Desktop.

Select an option

Save praveenb/3b8e6af14c26b5211d9d30adaebd83b1 to your computer and use it in GitHub Desktop.
How to add custom font in Android WebView.
//View container..
View rootView = inflater.inflate(R.layout.fragment1, container, false);
//Global WebView
mWebView = (WebView) rootView.findViewById(R.id.enter_text);
//Font must be placed in assets/fonts folder
String text = "<html><style type='text/css'>@font-face { font-family: spqr; src: url('fonts/spqr.ttf'); } body p {font-family: spqr;}</style>"
+ "<body >" + "<p align=\"justify\" style=\"font-size: 22px; font-family: spqr;\">" + getString(R.string.enter_text) + "</p> "+ "</body></html>";
mWebView.loadDataWithBaseURL("file:///android_asset/",text,"text/html","utf-8",null);
return rootView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment