How to set text size in WebView in android How to set text size in WebView in android android android

How to set text size in WebView in android


For setting text size from layout-

final WebSettings webSettings = web.getSettings();Resources res = getResources();fontSize = res.getDimension(R.dimen.txtSize);webSettings.setDefaultFontSize((int)fontSize);

For Immediate text display-

webSettings.setRenderPriority(RenderPriority.HIGH);webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);webSettings.setAppCacheEnabled(false);webSettings.setBlockNetworkImage(true);webSettings.setLoadsImagesAutomatically(true);webSettings.setGeolocationEnabled(false);webSettings.setNeedInitialFocus(false);webSettings.setSaveFormData(false);

In values folder-

<resources>    <dimen name="txtSize">26sp</dimen></resources>

Hope it works.


WebSettings webSettings = webView.getSettings();

either setTextSize or

webSettings.setTextSize(WebSettings.TextSize.SMALLEST);

This one works too:

webSettings.setDefaultFontSize(10);

And Display text immediately:

webview.getSettings().setRenderPriority(RenderPriority.HIGH);webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);webview.setBlockNetworkImage(true);webview.setLoadsImagesAutomatically(true);webwebviewSettings.setGeolocationEnabled(false);webview.setNeedInitialFocus(false);webview.setSaveFormData(false);


I'm not sure if this is what you're looking for, but as long as you don't want to set the text size based on a calculation with the text width/height, this should be what you're looking for:

WebSettings settings = yourWebView.getSettings();

This way you're getting the settings of the WebView whose text size you're trying to change.

There're some predefined values you can set as size using the TextSize class, for instance:

settings.setTextSize(TextSize.SMALLEST);settings.setTextSize(TextSize.SMALLER);settings.setTextSize(TextSize.NORMAL);settings.setTextSize(TextSize.BIGGER);settings.setTextSize(TextSize.BIGGEST);

You could simply choose which one you need to set depending on the parameters of the current device that you choose.

If you, on the contrary, want to make some kind of screen width/height calculation and set the result as a particular value, you may even use this:

settings.setDefaultFontSize(an_integer_that_goes_between_1_and_72);

---- EDIT ----

To enhace your WebView rendering, try the following:

mWebView.getSettings().setRenderPriority(RenderPriority.HIGH);mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

You could also try adding this to your Manifest file:

android:hardwareAccelerated="true"