How to Change the Fontsize in an Android Webview

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.

How can i change my font size when loading webview?

Try

  1. Add <bool name="isTablet">false</bool> to values/dimens.xml

    <resources>

    <bool name="isTablet">false</bool>

    </resources>
  2. Add <bool name="isTablet">true</bool> to values-sw600dp/dimens.xml

    <resources>

    <bool name="isTablet">true</bool>

    </resources>
  3. Add following code to your activity:

    if (context.getResources ().getBoolean (R.bool.isTablet)) {
    web.getSettings ().setTextZoom (120); //large font for tablet
    } else {
    web.getSettings ().setTextZoom (100); //default for phone
    }

Adjust font size of Android WebView

@rob's answer and comment pointed me in the right direction.

First make sure that all font sizes are relative to the default font size. If you use absolute values the following will not work on those elements.

Then:

@Override
protected void onCreate(Bundle savedInstanceState) {
...
mWebView = (WebView) findViewById(R.id.webview);
fontSize = mWebView.getSettings().getDefaultFontSize();
...
}

private void fontSizePlus() {
fontSize++;
this.changeFontSize(fontSize);
}

private void fontSizeMinus() {
fontSize--;
this.changeFontSize(fontSize);
}

private void changeFontSize(int value) {
mWebView.getSettings().setDefaultFontSize(value);
}

By changing the value of the default font size all relative font sizes are adjusted accordingly. This gives you much greater control than WebSettings.setTextSize (WebSettings.TextSize t).

change textsize in android webview

Use this to zoom the text size

WebSettings settings = mWebView.getSettings();
settings.setTextZoom(settings.getTextZoom() + 10);

or use

WebSettings settings = mWebView.getSettings();
settings.setTextSize(WebSettings.TextSize.LARGER);

How to set font size and text color in WebView?

It is best to do theme activities using CSS and Javascript. However if we want to pass on some settings from Android to the WebView dynamically, it is possible and a solution is to use the JavascriptInterface. Here is one way of doing it:

Firstly, we define a class which will be used as a bridge between the Android app and the WebView for JS interactions.

Here WebInterface is an inner class in the Activity and hence it has direct access to myWebView, which is a WebView instance variable.

        public class WebInterface {
private Activity activity;

public WebInterface(Activity activiy) {
this.activity = activiy;
}

@JavascriptInterface
public void changeTheme() {

activity.runOnUiThread(new Runnable() {

@Override
public void run() {
// All of the theme settings could go here, the settings passed on by Android
myWebView.loadUrl("javascript:document.body.style.backgroundColor ='red';");
myWebView.loadUrl("javascript:document.body.style.fontSize ='20pt'");
myWebView.loadUrl("javascript:document.body.style.color ='yellow';");

//OR load your data as shown here http://stackoverflow.com/a/7736654/891092

htmlData = "<link rel=\"stylesheet\" type=\"text/css\" href=\"theme.css\" />" + htmlData;
// lets assume we have /assets/theme.css file
myWebView.loadDataWithBaseURL("file:///android_asset/", htmlData, "text/html", "UTF-8", null);
}
});
}
}

Note that it is very important to run your code in UI Thread otherwise it will not work.

Here is how the Activity registers the WebView with the JavascriptInterface:

myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(jsInterface, "JSInterface");

In the HTML file, which the user is viewing, a button or widget could be made to change theme by calling code in Android through the bridge:

<input type="button" value="Say hello" onClick="doChangeTest()" />
<script type="text/javascript">
function doChangeTest(){
JSInterface.changeTheme(); // this calls the changeTheme in WebInterface
}

</script>



Related Topics



Leave a reply



Submit