Text color in webview

Big JR

Member
Licensed User
Longtime User
I can change webview background color outside of the html file with WebView1.Color = Colors.xxxx.

Is there a way to also change webview default text color similarly outside of the html file?

I want to give users the option of either black text on white or white text on black and would rather not maintain two html versions.
 
It seems strange that the background colour of the webview would still be displayed after a page has been loaded.

Regardless, if you use warwounds excellent WebViewExtras library you can execute javascript on the page.

Something like:
Code: Copy
Sub Button1_Click
wve.executeJavascript(webView1, "document.style.color='white'")
wve.executeJavascript(webView1, "document.style.color='black'")
End Sub
 
Upvote 0
Doing something wrong

Thanks admac231 but I'm doing something wrong. I've added WebViewExtras and ensured it's ticked and loaded.

I've dim'd wve as WebView Extras in Globals.

I've added
wve.executeJavascript(WebView1, "document.style.color='white'")
before loading url that points to the html file.

Text is still black.

I've tried moving the executeJavascript line to after the webview page has been loaded and still black text.

What am I doing wrong?
 
Upvote 0
You need to use javascript to update the page style after it has fully loaded i think:

Code: Copy
Sub MyWebView_PageFinished(Url As String)
    wve.executeJavascript(WebView1, "document.style.color='white'")
End Sub

That assumes wve is a global reference to WebViewExtras.

Martin.
 
Upvote 0
This seems to work for me..
Code: Copy
Sub Globals
Dim wve As WebViewExtras
Dim WebView1 As WebView
End Sub

Sub webView1_PageFinished(Url As String)
 wve.executeJavascript(WebView1,"document.body.style.backgroundColor='black'")
 wve.executeJavascript(WebView1,"document.body.style.color='white'")
End Sub
 
Upvote 0
Fabulous

Fabulous. Many thanks. And I've added:
wve.executeJavascript(WebView1,"document.linkColor='lightblue'")
to make links more readable on a black background.
 
Upvote 0
Just been trying this, to do a 'Night Mode', but doesn't seem to be working.
Does this still work with B4A 2.02 for those who've tried it before?

Edit: even 'WebView1.Color = Colors.Black' doesn't work

Unless I'm missing something.

Edit 2: WebView1.Color is working, it has to be done before the LoadUrl.

Edit 3: Found what I was missing. Using the 'document.body.style...' as used by varbello in post 7 worked. (Can't think why I didn't try it.)
 
Last edited:
Upvote 0
Back
Top