Reputation: 5440
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
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