1

I'm trying to create a night mode for my app which triggers on Button click which is a View from Android. on click I wanted the webview to change it's background color and the font color using a custom style via CSS but I don't have the idea on how to do it. I do know how to create a Javascript but I don't know on how I can implement it on my app since what I need is for the background to change without reloading the page. I tried to read some questions which has seems to be similar to my problem but none of it really sinks in to me and It's not that well detailed for me to understand.

Furthermore, my data is created on beforehand using

content.loadDataWithBaseURL(null, MY_HTML_CODE, "text/html", "utf-8", null);

so I can inject some script in case.

I'm not sure on how I can relate it on this answer from another question either which just confuses me even more.

Hope someone can help.

1 Answer 1

3

I already found the solution to my problem which is to just manually set the javascript onClick of button like this:

@onClick(R.id.btn_change)
    public void changeMode(){
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            webView.evaluateJavascript("document.body.style.backgroundColor=\"black\";document.body.style.color=\"white\";", null);
        } else {
            webView.loadUrl("javascript:document.body.style.backgroundColor=\"#black\";document.body.style.color=\"white\";");
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

How does it work ? i tried to add the evaluateJavascript to change my webview but nothing change

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.