1

I want to apply my custom CSS on HTML page i tried this way but it is not working.

 wv.loadUrl("<style>.featured {"+
        "   background-image: -ms-linear-gradient(bottom, #F9F6F9 0%, #ECE4F4 100%);"+
        "   background-image: -moz-linear-gradient(bottom, #F9F6F9 0%, #ECE4F4 100%);"+
        "   background-image: -o-linear-gradient(bottom, #F9F6F9 0%, #ECE4F4 100%);"+
        "background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0, #F9F6F9), color-stop(1, #ECE4F4));"+
        "   background-image: -webkit-linear-gradient(bottom, #F9F6F9 0%, #ECE4F4 100%);"+
        "   background-image: linear-gradient(to top, #F9F6F9 0%, #ECE4F4 100%);"+
        "}"); 

please help me out.

3 Answers 3

1

Thank you for your answers guys but i got my solution.

This my solution.

wv.loadUrl("javascript:(function() { " +                        
    "var divs = document.getElementsByClassName('free');"+
     "for(var i=0; i<divs.length; i++)"+
     "{"+
        "divs[i].style.backgroundImage='-webkit-gradient(linear, left bottom, left top, color-stop(0, #FFFFFF), color-stop(1, #EFD2E4))';"+
      "}"+
     "})()");   

using this you can apply your custom CSS on any Tag or Elements of the HTML in Android.

Sign up to request clarification or add additional context in comments.

Comments

0

Put your html and css file inside the Assets folder. And use the following code.

Main.java

WebView webview = (WebView) findViewById(R.id.abtus_webView);
webview.loadUrl("file:///android_asset/index.html");

and if you want to use image into html page then add below code into html page.

htmltest.html

<img src="file:///android_asset/images/abc.png">

I have put the images into the images Folder in Assets folder.This is working for me correct,I hope it helps you.

Comments

0

You can inject custom JS by using the javascript: URL capability.

Here's how you can add CSS rules using this from Java:

/**
 * Creates a CSS element in the <head> section of the Web page and assigns it
 * to a `customSheet` JS variable
 */
private final static String CREATE_CUSTOM_SHEET =
    "if (typeof(document.head) != 'undefined' && typeof(customSheet) == 'undefined') {"
        + "var customSheet = (function() {"
            + "var style = document.createElement(\"style\");"
            + "style.appendChild(document.createTextNode(\"\"));"
            + "document.head.appendChild(style);"
            + "return style.sheet;"
        + "})();"
    + "}";

/**
 * Adds CSS properties to the loaded Web page. A <head> section should exist when this method is called.
 * The Web view should be configured with `.getSettings().setJavaScriptEnabled(true);`
 *
 * @param webView Web view to inject into
 * @param cssRules CSS rules to inject
 */
void injectCssIntoWebView(WebView webView, String... cssRules) {
    StringBuilder jsUrl = new StringBuilder("javascript:");
    jsUrl
        .append(CREATE_CUSTOM_SHEET)
        .append("if (typeof(customSheet) != 'undefined') {");
    int cnt = 0;
    for (String cssRule : cssRules) {
        jsUrl
            .append("customSheet.insertRule('")
            .append(cssRule)
            .append("', ")
            .append(cnt++)
            .append(");");
    }
    jsUrl.append("}");

    webView.loadUrl(jsUrl.toString());
}

And here's an usage example of the above method:

@Override
public void onPageFinished(WebView webView, String url) {
    // Several people probably worked hard on the design of this Web page, let's hope they won't see what's next
    injectCssIntoWebView(
        webView,
        "div { border: 4px solid yellow; }",
        "p { border: 4px solid green; }",
        "a { border: 4px solid black; }",
        "img { border: 4px solid blue; }"
    );
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.