I have been searching for days but could not find solution for my problem. Please kindly take a look:
- Input: a WebView with an already loaded web.
- Output: change the font of that loaded web inside the WebView.
What I have done so far is this:
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient());
mWebView.loadUrl("http://www.bbc.co.uk/");
and then, this is how I change the font inside webview: 1/ way 1:
mWebView.loadUrl("javascript:(function() { var s=document.createElement('style');s.innerHTML ="
+ " '@font-face{font-family:ZawGyi-One;src:url(\"http://db.tt/OQ1RZoWc\");}"
+ "body,div,h1,h2,h3,input,textarea{font-family:ZawGyi-One! important;}';"
+ "document.getElementsByTagName('head')[0].appendChild(s);document.getElementsByTagName('body')[0].style.fontFamily = \"ZawGyi-One\"})()");
2/ way 2:
mWebView.loadDataWithBaseURL(
null,
"<script>javascript:(function() { var s=document.createElement('style');s.innerHTML ="
+ " '@font-face{font-family:ZawGyi-One;src:url(\"http://db.tt/OQ1RZoWc\");}"
+ "body,div,h1,h2,h3,input,textarea{font-family:ZawGyi-One! important;}';"
+ "document.getElementsByTagName('head')[0].appendChild(s);document.getElementsByTagName('body')[0].style.fontFamily = \"ZawGyi-One\"})() </script>",
"text/html", "UTF-8", null);
3/ way 3:
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebView.loadUrl("javascript:(function() { var s=document.createElement('style');s.innerHTML ="
+ " '@font-face{font-family:ZawGyi-One;src:url('file:///android_asset/fonts/BLKCHCRY.TTF');}"
+ "body,div,h1,h2,h3,input,textarea{font-family:ZawGyi-One! important;}';"
+ "document.getElementsByTagName('head')[0].appendChild(s);document.getElementsByTagName('body')[0].style.fontFamily = \"ZawGyi-One\"})()");
}
});
But unfortunately, none of my solutions works. Even though I have tried to change html file insdie /assets/ folder and also the fonts are stored at /assets/fonts/ folder, I cannot walk through this.
**One more question: is it really possible to change font style inside an already loaded site of webview ? Someone, please kindly guide me.
I am appreciated your great help.