1

Ok so what i do is the following:

i have a html string i show in a WebView like this:

webView.loadDataWithBaseURL("",htmlText, "text/html","utf-8", null);

i also have a local css file i want to apply to that html String in the webView.

i looked this up for a while but no solution seemed to work.

There should be a way to save the css file in the assets folder and call it from somwhere but i dont have an idea how.

and suggestions?

2 Answers 2

2

You can't write anything in the assets folder.

Since you have both the html and the css, I think you can insert css text into the head tag some how like this way

int pos = htmlText.toLowerCase().indexOf("</head>");
if(pos != -1) {
    String newHtmlText = htmlText.substring(0, pos) + String.format("<style>%s</style>", cssText) + htmlText.substring(pos + "</head>".length());
    webView.loadDataWithBaseURL("",newHtmlText , "text/html","utf-8", null);
}

disclaimer: untested :))

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

1 Comment

since i didnt have all the tags i added them as in my answer below. but you are correct, this is how to do it. Thanks.
0

Since i didnt have a nor nor tag i just added them and put the style in the and my html-text in a like this:

String htmlWithCss = "<html><head><style type=\"text/css\">" +css +"</style></head><body>" + htmlText +"</body></html>";

and displayed it in the webView like this:

webView.loadDataWithBaseURL("", htmlWithCss, "text/html","utf-8", null);

This is just to show how to do it when you have blank html-formated text wothout tags around it. i will acceppt J.S. Taylor's answer nevertheless since it showed me basicly what to do.

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.