Tuesday, 29 January 2013

Android Web View with Progress Dialog.

How to show Progress Dialog while loading a page to WebView using WebClient.
























public final class MyWebview extends Activity {

private static final int DIALOG2_KEY = 1;
private ProgressDialog pd = null;
private static final String  AmitBlog="http://amitandroid.blogspot.in/";
private WebView webView;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.help_page);
pd = new ProgressDialog(this);
webView = (WebView) findViewById(R.id.help_contents);
webView.setWebViewClient(new HelpClient());
webView.getSettings().setBuiltInZoomControls(true);

  /** Showing Dialog Here */
showDialog(DIALOG2_KEY);
}

@Override
protected void onResume() {
                super.onResume();
LoadView();
}

private void LoadView()
{
webView.loadUrl(AmitBlog);
}

 /** Its very important while navigation hardware back button if we navigate to another link .Its like a Stack pop of all the pages you visited in WebView */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (webView.canGoBack()) {
webView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}

/** WebViewClient is used to open other web link to the same Activity. */

private final class HelpClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
setTitle(view.getTitle());

   /** Dismissing Dialog Here after page load*/
dismissDialog(DIALOG2_KEY);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("file")) {
return false;
} else{
view.loadUrl(url);
return true;
}
}
}

@Override
protected Dialog onCreateDialog(int id) {
switch (id)
{
case DIALOG2_KEY:
{
pd.setMessage(getResources().getString(R.string.Loading));
pd.setIndeterminate(true);
pd.setCancelable(false);
return pd;
}
}
return null;
}
}

Click here to Download  Code

3 comments:

  1. Bro Please Send Me Ur MAil ID at

    poojesh123@gmail.com

    ReplyDelete
  2. I simply want to say I’m very new to blogs and actually loved you’re blog site. Almost certainly I’m going to bookmark your blog post . You absolutely come with great well written articles. Thanks a lot for sharing your blog.
    Android Training institute in chennai with placement | Android Training in chennai

    ReplyDelete
  3. Thank you for sharing valuable information about android. It is very useful to android learners, please keep updating information regarding Android app development course .

    ReplyDelete