How to set font type in webview

I found many questions to set the font type in webview. I tried, I can’t fix it.

I have 10 html files in my folder. In scrolling im, showing one by one in webview, Before that it works fine, now I need to change the font type of this on the click button (for all html pages that im load 10 files). How can I change the font type of my html display in webview.

First way

mainContent.getSettings().setJavaScriptEnabled(true); WebSettings webSettings = mainContent.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setBuiltInZoomControls(true); mainContent.requestFocusFromTouch(); mainContent.setWebViewClient(new WebViewClient()); mainContent.setWebChromeClient(new WebChromeClient()); mainContent.loadUrl("file:///android_asset/"+filename.get(position)); Typeface font = Typeface.createFromAsset(getAssets(), "myfont.ttf"); webSettings.setFixedFontFamily(font); webSettings.setDefaultFontSize(20); 

Second way

I get the html content and I went to the line, I cannot go into it, if the content is passed correctly, this will work.

  mainContent.getSettings().setJavaScriptEnabled(true); WebSettings webSettings = mainContent.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setBuiltInZoomControls(true); mainContent.requestFocusFromTouch(); mainContent.setWebChromeClient(new WebChromeClient()); mainContent.loadUrl("file:///android_asset/"+filename.get(position)); mainContent.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); mainContent.setWebViewClient(null); mainContent.loadUrl("javascript:window.HTMLOUT.processHTML('<div>'+document.getElementsByTagName('div')[0].innerHTML+'</div>');"); String pish = "<html><head><style type=\"text/css\">@font-face {font-family: MyFont;src: url(\"file:///android_asset/fonts/Kelvetica.ttf\")}body {font-family: MyFont;font-size: medium;text-align: justify;}</style></head><body>"; String pas = "</body></html>"; String myHtmlString = pish + page + pas; // here i can't able to get the `page` mainContent.loadDataWithBaseURL(null,myHtmlString, "text/html", "UTF-8", null); 

and in my work

  class MyJavaScriptInterface { @SuppressWarnings("unused") public void processHTML(final String html) { runOnUiThread(new Runnable() { public void run() { Spanned page = Html.fromHtml(html); System.out.println("content"+page); } }); } } 

I need to pass this page to the code above

Third way

  mainContent.getSettings().setJavaScriptEnabled(true); WebSettings webSettings = mainContent.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setBuiltInZoomControls(true); mainContent.requestFocusFromTouch(); mainContent.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT"); mainContent.loadUrl("file:///android_asset/"+filename.get(position)); mainContent.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); mainContent.setWebViewClient(null); mainContent.loadUrl("javascript:(function() { var s=document.createElement('style');s.innerHTML =" + " '@font-face{font-family:ZawGyi-One;src:url('file:///android_asset/fonts/zeroesthree.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\"})()"); } }); 

I tried like this. I can not change the font. Anyone have an idea, please share it. I have been working on this issue for the last two days. Thanks at Advance.

+6
source share
1 answer

You can do this in two ways.

  • If your button is part of html, you can call the JavaScript function in the click event
  • Using the Android button, you can call the JavaScript function, which will update the font family.

Here is an example: CSS font_first.css:

 .box_heading{ width:340px; text-align:center; font-family:Arial, Helvetica, sans-serif; color:#000; font-size:20px; font-weight:normal; margin-top:14px; float:left; text-transform:uppercase;} 

Html: index.html

If you do this with the second method, you just need to call changeFont() from the android website for this, you need to call mainContent.loadUrl("javascript:changeFont()")

EDIT
If so, you can try the following: myWebView.loadUrl("javascript:document.getElementById('navigationText')style.fontFamily ='"+fontName+"');

+4
source

Source: https://habr.com/ru/post/972306/


All Articles