Android WebView Introduction

android.webkit.WebView is used to load and render html web pages in android app. It is commonly used in hybrid android application. The web page can be one of following.

1. How to create and destroy WebView widget.

There are two methods to obtain a WebView object.

  1. Define the WebView in layout xml file, and use Activity’s findViewById method to get it.
    webView = (WebView)findViewById(R.id.web_view_component);
  2. Create the WebView object in activity java code.
    WebView webView = new WebView(this);
  3. Do not forget destroy WebView object when your activity is destroyed, this can save cpu resources and avoid memory leak.
    if(webView!=null)
    {
        ViewGroup viewGroup = (ViewGroup) webView.getParent();
        viewGroup.removeView(webView);
    
        webView.destroy();
        webView = null;
    }

2. How to load html data in WebView.

  1. Url : http://yahoo.com, must start with http or https.
    webView.loadUrl("http://yahoo.com");
  2. Local html file :  file://sdcard/index.html.
    webView.loadUrl("file://sdcard/index.html");
  3. A content provider uri : content://com.dev2qa.example/abc/test.html.
    webView.loadUrl("content://com.dev2qa.example/abc/test.html");
  4. A snippet of html code : <b>hello world</b>.
    webView.loadData("<b>hello world</b>", "text/html", "utf-8");

3. WebView required permissions.

Because WebView will access internet, so it need INTERNET permission. So you should declare below permission in AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET" />

4. How to configure WebView object.

android.webkit.WebSettings is the class that can configure WebView object’s options. WebView’s getSettings method return it. WebSettings class contains below methods.

  1. setJavaScriptEnabled(boolean enabled) : Enable or disable javascript. Commonly when the webview’s onPause method is invoked, we set it to false. When webview’s onResume method is called, we set it to true. This can save resources such as cpu. memory etc.
  2. setPluginsEnabled(boolean enabled) : Similar with setJavaScriptEnabled.
  3. setSupportZoom(boolean enabled) : Support zoom or not.
  4. setCacheMode(int cacheMode) : Set cache mode, the value can be WebSettings.LOAD_CACHE_ONLYLOAD_DEFAULTLOAD_NO_CACHE and LOAD_CACHE_ELSE_NETWORK. When webview load html data, it will generate database and cache folder under /data/data/< package name>/. The requested urls is saved in WebViewCache.db, and url page data is saved in WebViewCache folder.
  5. setLoadsImagesAutomatically(boolean value) : Set whether load images auto or not.
  6. setUseWideViewPort(boolean value) : Adjust image size to fit webview size.
  7. setJavaScriptCanOpenWindowsAutomatically(boolean value) : Set whether can use javascript to open new window or not.
  8. setLoadWithOverviewMode(boolean value) :  Zoom web page to the screen size or not.
  9. setAllowFileAccess(boolean value) : Set whether can access file or not.
  10. setDefaultTextEncodingName(“utf-8”) : Set default webview text encoding format.

5. How to intercept and process web page load event with WebView.

android.webkit.WebViewClient and android.webkit.WebChromeClient class provide methods for you to intercept and process page load event while webview load page.

You can extends them and override required methods which you want to process. Then you can call WebView‘s setWebViewClient and setWebChromeClient method to set the webview client instance to it.

5.1 WebViewClient’s method.

  1. onPageStarted : Invoked when page load start.
  2. onPageFinished : Invoked when page load complete.
  3. shouldOverrideUrlLoading : Return false means open html link page in current webview object, return true means call system web browser to browse link page.
  4. onReceivedError : Triggered when webview receive an error from server during access page url.
  5. onReceivedSslError : WebView will not process https request, and display a blank page. This method is called when process https request.

5.2 WebChromeClient method.

  1. onProgressChanged : Invoked when WebView load page not complete, you can show the progress value in a progress bar.
  2. onReceivedTitle : This method is used to get web page title, you can display this title in android activity window title.
  3. onJsAlert : Intercept javascript alert dialog in web page, commonly create an android AlertDialog to display the javascript alert message.
  4. onJsConfirm : Similar with onJsAlert, the difference is intercept javascript confirm dialog.
  5. onJsPrompt : Similar with onJsConfirm. This method can intercept javascript prompt dialog.

Reference

  1. Android WebView Examples

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.