Display progress alert dialog on WebView loading

Show progress alert dialog to loading webView in background task then hide progress alert and show website.

Progress alert dialog is used to display simple alert dialog while some task is completing in background in android apps. So in this tutorial i am creating a progress dialog and showing it on activity screen then loading webview in background and after done loading website into webview hiding progress dialog and displaying website. So here is the complete step by step tutorial for Display progress alert dialog on WebView loading.

android-project-download-code-button

Note: Please add internet permission on AndroidManifest.xml file.

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

How to Display progress alert dialog on WebView loading.

Code for MainActivity.java file.

 package com.android_examples.com.webviewprogressdialog;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;


public class MainActivity extends Activity {

 WebView webView;
 ProgressDialog Pdialog;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 webView = (WebView)findViewById(R.id.webView1);
 webView.setWebViewClient(new WebViewClient());
 webView.loadUrl("https://www.google.com");
 
 }
 
 public class WebViewClient extends android.webkit.WebViewClient
 {
 @Override
 public void onPageStarted(WebView view, String url, Bitmap favicon) {

 // TODO Auto-generated method stub
 super.onPageStarted(view, url, favicon);
 
 Pdialog = new ProgressDialog(MainActivity.this);
 Pdialog.setTitle("LOADING");
 Pdialog.setMessage("Your page is loading...");
 Pdialog.show();
 }
 
 @Override
 public boolean shouldOverrideUrlLoading(WebView view, String url) {

 // TODO Auto-generated method stub
 view.loadUrl(url);
 return true;
 }
 @Override
 public void onPageFinished(WebView view, String url) {

 // TODO Auto-generated method stub

 super.onPageFinished(view, url);
 Pdialog.dismiss();

 }

 }
 
}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.android_examples.com.webviewprogressdialog.MainActivity" >

 <WebView
 android:id="@+id/webView1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:layout_alignParentTop="true"
 android:layout_centerHorizontal="true" />

</RelativeLayout>

Code for AndroidManifest.xml file.

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.android_examples.com.webviewprogressdialog"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk
 android:minSdkVersion="8"
 android:targetSdkVersion="21" />

 <uses-permission android:name="android.permission.INTERNET" />
 
 <application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
 android:name=".MainActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 </application>

</manifest>

Screenshots:

Display progress alert dialog on WebView loading

done loading webview

Click Here To Download Display progress alert dialog on WebView loading project.