Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Customizing a WebView

Author: Rachee Singh
Published? true
FormatLanguage: WikiFormat

Problem:

You need to customize the WebView opened by your application.

Solution:

Using the WebSettings class for accessing inbuilt functions for customizing the browser.

Discussion:

As discussed in the recipe titled Accessing a web page through your Android application, to open a web page in an Android application we use a WebView component. Then to load a URL in the WebView we use:

webview.loadUrl("http://www.google.com/");

Now, to customize the browser to suit the user's needs there are multiple actions that can be taken. We need to instantiate the WebSettings class:

WebSettings webSettings = webView.getSettings();

Here are a few things that can be done using WebSettings:

  1. Tell the WebView to block network images:
webSettings.setBlockNetworkImage (true);
  1. Set the default font size in the browser:
webSettings.setDefaultFontSize(25);
  1. Set whether the WebView supports zoom:
webSettings.setSupportZoom(true);
  1. Tell the WebView to enable javascript execution:
webSettings.setJavaScriptEnabled(true);
  1. Store whether the WebView is saving password.
webSettings.setSavePassword(false);
  1. Store whether the WebView is saving form data.
webSettings.setSaveFormData(false);

Many more methods of this kind are available. For more information, see the Android developers page on the topic.

No records found.