Android examples for android.webkit:WebView
inject CSS to WebView
import android.annotation.SuppressLint; import android.content.Context; import android.os.Build; import android.util.Base64; import android.webkit.WebSettings; import android.webkit.WebView; import java.io.InputStream; public class Main{ @SuppressLint("SetJavaScriptEnabled") public static void injectCSS(Context context, WebView webView, String filename) {//from w w w . j a v a 2 s . c om try { webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setAllowFileAccess(true); webView.getSettings().setDatabaseEnabled(true); webView.getSettings().setDomStorageEnabled(true); webView.getSettings().setSaveFormData(false); webView.getSettings().setAppCacheEnabled(true); webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); webView.getSettings().setLoadWithOverviewMode(false); webView.getSettings().setUseWideViewPort(true); InputStream inputStream = context.getAssets().open(filename); byte[] buffer = new byte[inputStream.available()]; inputStream.read(buffer); inputStream.close(); String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP); webView.loadUrl("javascript:(function() {" + "var parent = document.getElementsByTagName('head').item(0);" + "var style = document.createElement('style');" + "style.type = 'text/css';" + // Tell the browser to BASE64-decode the string into your script !!! "style.innerHTML = window.atob('" + encoded + "');" + "parent.appendChild(style)" + "})()"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { WebView.setWebContentsDebuggingEnabled(true); } } catch (Exception e) { e.printStackTrace(); } } }