1

Hi all I am using webview in my application. I have css file which sets background color, font size. Now I want to set color and font size at runtime. How to override these properties at runtime in android? I had tried like:

webview.setBackgroundColor(Color.parseColor("#919191"));
WebSettings webSettings = webView.getSettings();
webSettings.setDefaultFontSize(25);

But it does not work. I had also tried to written this code after webView.loadDataWithBaseURL(......)

How to override font and bacground color at runtime?

1 Answer 1

1

The settings you mention are defaults. They are only applied if the page doesn't specify anything by itself.

You can manipulate the text size at runtime by using WebSettings.setTextZoom (doc) or WebSettings.setMinimumFontSize (doc).

There is no API for manipulating background color of the page. The best you can do is running JavaScript code that will do that after the page has loaded. For example:

webview.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView view, String url){
        view.loadUrl("javascript:document.body.style.backgroundColor = 'black';void(0);");
    }
});        
Sign up to request clarification or add additional context in comments.

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.