Basim Sherif
Basim Sherif

Reputation: 5440

Font size adjusting function in Android Webview

I have been developing an Android RSS news reader.I want to add two buttons for adjusting the font size when a news is loaded in Webview.Can anyone tell me, how can I create a function for changing the font size of Webview?

Upvotes: 2

Views: 3960

Answers (1)

Brian Cooley
Brian Cooley

Reputation: 11662

A WebView has a WebSettings object associated with it. You can get the object by calling getSettings on your WebView. The WebSettings object has a method called setTextZoom(int) which allows you to set the text size. The int argument is the percentage zoom and defaults to 100.

So, maybe something like this:

increaseFontButton.setOnClickListener( new OnClickListener() {
    public void onClick(View v) {
        WebSettings settings = myWebView.getSettings();
        settings.setTextZoom( (int)(settings.getTextZoom() * 1.1) );
    }
}

And something similar to decrease the font.

edit: I should have mentioned that setTextZoom and getTextZoom were introduced in API 14. If you are targeting prior to that API, you should use settings.setTextSize(WebSettings.TextSize) in which you pass in one of the WebSettings.TextSize enums (SMALLEST, SMALLER, NORMAL, LARGER, or LARGEST).

Upvotes: 7

Related Questions