Matt McMinn
Matt McMinn

Reputation: 16291

Android WebView scaled font size

In my application I have a dimension for a font defined as "18sp". This font size is used in both a TextView (as a header) and a WebView (as body text). On a mdpi device, both fonts look like they are the same size; however on a nexus 10 (xhdpi), the TextView looks correct while the WebView size doubles. I'm setting the text size on the webview using

int fontSize = res.getDimensionPixelSize(R.dimen.textSize);

then setting the font using css in the body:

<body style='font-size: " + fontSize + "px'>

It's obvious why the text size is doubling - 18sp on an xhdpi device comes out to 36px - but I'm confused as to why this is handled automatically by the TextView and not by the WebView. I can think of a couple of ways to handle this, but I don't know which (if any) is the right way:

Any ideas? Thanks

Upvotes: 1

Views: 4565

Answers (2)

Joe Average
Joe Average

Reputation: 11

You can use JavaScript code to change WebView style for fontSize, backgroundColor and font color. it's working.

loadUrl("javascript:(document.body.style.fontSize ='11pt');");
loadUrl("javascript:(document.body.style.backgroundColor ='red');");
loadUrl("javascript:(document.body.style.color ='yellow');");

Upvotes: 1

NagarjunaReddy
NagarjunaReddy

Reputation: 8645

set font size like this in webview

WebSettings settings = webview.getSettings();
settings.setDefaultFontSize(10);

Upvotes: 0

Related Questions