There's a method for altering background color but not font.
Any ideas?
Asked
Active
Viewed 3.8k times
8 Answers
40
something like
String text = "<html><head>"
+ "<style type=\"text/css\">body{color: #fff; background-color: #000;}"
+ "</style></head>"
+ "<body>"
+ your_string_text_here
+ "</body></html>";
webview1.loadData(text, "text/html", "utf-8");

OWADVL
- 10,704
- 7
- 55
- 67
25
I had to put it in the onPageFinished method.
_webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
_webView.loadUrl(
"javascript:document.body.style.setProperty(\"color\", \"white\");"
);
}
});

djunod
- 4,876
- 2
- 34
- 28
-
partially work, some of the font color still not overwritten – Beeing Jk Mar 20 '18 at 04:04
-
@BeeingJk that is because the font is overridden by the site – Sambhav Khandelwal May 21 '22 at 06:02
13
This worked for me
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
view.loadUrl(
"javascript:document.body.style.setProperty(\"color\", \"white\");"
);
}
});

Ismail Iqbal
- 2,774
- 1
- 25
- 46
-
With recent changes in this area, https://developer.android.com/about/versions/13/behavior-changes-13#webview-color-theme, I wish there was a way to accomplish this without enabling JavaScript and trigger static code analysis warnings – Alix Aug 22 '22 at 09:18
12
I'm not sure I understand. The WebView just displays the HTML you give it so you would just use normal HTML/CSS to modify the content displayed within.

MattC
- 12,285
- 10
- 54
- 78
-
-
Is the underlying HTML something from a third party? You could always inject javascript at the end that will modify it and then call the javascript from the Activity itself doing wvMyWebView.loadUrl("call javascript function here"); – MattC Aug 10 '09 at 18:05
-
1@MattC what if you change the app theme and you want to see changes instantly. – Faisal Shaikh Jul 08 '20 at 00:52
6
This is the easiest way I found (change the text color to white for example):
webview.loadUrl("javascript:document.body.style.color=\"white\";");

RafaelJan
- 3,118
- 1
- 28
- 46
-
-
note that you will have to enable javascript for the web view in question, otherwise the call will have no effect – sfera Sep 15 '14 at 08:25
2
@rafraph's answer didn't work for me. I had to use
webView.loadUrl("javascript:document.body.style.setProperty(\"color\", \"white\");");

Jess Smith
- 167
- 1
- 8
2
When the buffer is SPANNABLE, modifying the HTML directly is an ideal solution. The font, color, typeface, style can all be affected through HTML:
String szMessage = "<font face='trebuchet' size=30><a href=zz><b>click me</b></a></font>";
TextView tv = (TextView)findViewById(R.id.tv_message);
tv.setText(Html.fromHtml(szMessage), BufferType.SPANNABLE);

paiego
- 3,619
- 34
- 43
1
You can concatenate your answer one body tag HTML with CSS style hex color, this is an example using a JSON response
First: function for decode JSON to HTML format
public String stripHtml(String html) {
return Html.fromHtml(html).toString();
}
Second: Load data in WebView (no url)
String string_html;
string_html = "<body style="color:#535362;">" + youStringHTML + "</body>";
webView.loadDataWithBaseURL(null, stripHtml(string_html), "text/html", "utf-8", null);

Vladimir Salguero
- 5,609
- 3
- 42
- 47