Search This Blog

Saturday 17 November 2012

Android Webview example with progress dialog

In android, Webview is subclass of a view, and used for purpose to open a link inside our own custom application. As Webview is sub class of view, so we can used it where ever we want either using xml file or creating dynamically.

If i create this dynamically,

WebView webView = new WebView(this);
webView.setClickable(true);
webView.setFocusableInTouchMode(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
setContentView(webView);

Now this will load a URL http://www.google.com if you have Internet connection and registered Internet permission inside android-manifest. But our main issue remain to make Webview more responsive. Until Webview load url , we will show a progress dialog when page start loadeding, And dismissed the progress dialog after page loaded. WebViewClient class provide a method to detect when page load start and when it finished.

So Lets make one Inner class

 
 public class WebClientClass extends WebViewClient {
  ProgressDialog pd = null;

  @Override
  public void onPageStarted(WebView view, String url, Bitmap favicon) {
   super.onPageStarted(view, url, favicon);
   pd = new ProgressDialog(WebViewDemo.this);
   pd.setTitle("Please wait");
   pd.setMessage("Page is loading..");
   pd.show();
  }

  @Override
  public void onPageFinished(WebView view, String url) {
   super.onPageFinished(view, url);
   pd.dismiss();
  }
 }

This Inner class can be attach to Webview to maintain progress dialog box life cycle WebClientClass webViewClient = new WebClientClass(); webView.setWebViewClient(webViewClient); One another important class is WebChromeClient, that handle JsAlert, Security Error and many other function. Add that also as same as above procedure

Combining all effort will look like this 



package com.example.webviewtag;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewDemo extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  WebView webView = new WebView(this);
  webView.setClickable(true);
  webView.setFocusableInTouchMode(true);
  webView.getSettings().setJavaScriptEnabled(true);
  webView.loadUrl("http://www.google.com");
  WebClientClass webViewClient = new WebClientClass();
  webView.setWebViewClient(webViewClient);
  WebChromeClient webChromeClient=new WebChromeClient();
  webView.setWebChromeClient(webChromeClient);
  setContentView(webView);
 }

 public class WebClientClass extends WebViewClient {
  ProgressDialog pd = null;

  @Override
  public void onPageStarted(WebView view, String url, Bitmap favicon) {
   super.onPageStarted(view, url, favicon);
   pd = new ProgressDialog(WebViewDemo.this);
   pd.setTitle("Please wait");
   pd.setMessage("Page is loading..");
   pd.show();
  }

  @Override
  public void onPageFinished(WebView view, String url) {
   super.onPageFinished(view, url);
   pd.dismiss();
  }
 }
 
 public class WebChromeClass extends WebChromeClient{
 }
}


Check Out Screen Shot Of application 

Webview
Loading..
Loaded
    

No comments:

Post a Comment

Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging

Android News and source code