How to Inject CSS to WebView Android Java

Insert or inject stylesheet css to Android webview java

You can stylize or modify webpages with your own custom CSS style sheet in Android webview. To do that, we need a stylesheet.css and a few lines of code.

In the below code, we will inject a css in the webview of Android after it finishes loading a website, but how do we know if a web page has finished loading already? Yes we can, with the onPageFinished method of Android.

This is the method that gets invoked once the Android webview finishes loading a website so we can do some custom action

public void onPageFinished(WebView view, String url)

Injecting the CSS into Webview after finish loading

First, put your css file inside the assets folder, rename your css to black or change the css name to your css name in the below code


@Override public void onPageFinished(WebView view, String url) {

                // Inject CSS when page is done loading
                injectCSS();           super.onPageFinished(view, url);

        // Load a webpage


    // Inject CSS method: read style.css from assets folder
// Append stylesheet to document head
    private void injectCSS() {
        try {
            InputStream inputStream = getAssets().open("black.css");
            byte[] buffer = new byte[inputStream.available()];
            String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
            webl.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)" +
        } catch (Exception e) {


The above code injects the CSS from assets folder (black.css) to webview after it has been loaded. i use this code for setting a night mode in my app. you need define the load url twice, first define it above the shouldOverrideUrlLoading method, then in the onpagefinished method (already defined in the code above)

A user requested the Black.css file, so here is it

Download Black.css

Update: If you are new to android development, or just want fix this error or other android app development errors, i can fix those for you for a reasonable small feeJust send me your project or that java file (The Activity where you wish to add this code) through email or google drive, i will fix your project or file and send it back to you.

Related Posts

Add Admob ads in recyclerview in android

How to add admob native ads to recyclerview in android java Adding admob native ads into android recyclerview isn’t easy like other admob ads. You will need…

Download WebView Flutter Source Code for Android and iOS

Easily create android and iOS WebView app for your websites, the much-awaited flutter WebView source code is here. Are you looking for a flutter WebView source code…

Fix uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:_flutter_android]

This error can be fixed by modifying build.gradle file in the flutter In android studio: go to android>app>build.gradle and modify it as follows:

Get Your Own Android Mobile App for Your Website Today

Are you looking to expand your online presence and reach more customers? In today’s digital age, having a mobile app is an essential tool to connect with…

Why making an app for your website can help your business to grow

In today’s digital age, it’s more important than ever to have a strong online presence. Whether you’re running a business, a blog, or a personal website, having…

Fix android.content.Intent.migrateExtraStreamToClipData(android.content.Context)’ on a null object reference

Here is how to Fix Android Studio Error Attempt to invoke virtual method ‘boolean android.content.Intent.migrateExtraStreamToClipData(android.content.Context)’ on a null object reference This error is caused by a number…

This Post Has 2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *