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.