I’ve seen a multitude of posts on how to embed the Android WebView object and how to use the built-in feature request PROGRESS, but I’ve yet to come across a demonstration on just how simple it is to integrate a ProgressDialog object. I specifically needed this for one of my projects because I did not want the title bar to render in my application. If the title is not rendered, then the Window Feature Request progress bar and indeterminant functions will not render at all.
So here is how to do it! Trust me it’s absolutely simple!
Code snippet basics:
import android.app.ProgressDialog; //in the class... private ProgressDialog progressBar; //when you want the dialog to show the first time. progressBar = ProgressDialog.show(Main.this, "MaxPowerSoft Example", "Loading..."); //when you want the progressbar to disappear if (progressBar.isShowing()) { progressBar.dismiss(); } |
Here is how it is done in your project.
/res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">MaxPowerSoft Example</string> <string name="webview">webview</string> </resources> |
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.maxpowersoft.example" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Main" 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> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="4" /> </manifest> |
/res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <WebView android:id="@string/webview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" /> </LinearLayout> |
Main.java
package com.maxpowersoft.example; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.Window; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class Main extends Activity { private WebView webview; private static final String TAG = "Main"; private ProgressDialog progressBar; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); this.webview = (WebView)findViewById(R.string.webview); WebSettings settings = webview.getSettings(); settings.setJavaScriptEnabled(true); webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); progressBar = ProgressDialog.show(Main.this, "MaxPowerSoft Example", "Loading..."); webview.setWebViewClient(new WebViewClient() { public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.i(TAG, "Processing webview url click..."); view.loadUrl(url); return true; } public void onPageFinished(WebView view, String url) { Log.i(TAG, "Finished loading URL: " +url); if (progressBar.isShowing()) { progressBar.dismiss(); } } public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Log.e(TAG, "Error: " + description); Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show(); alertDialog.setTitle("Error"); alertDialog.setMessage(description); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { return; } }); alertDialog.show(); } }); webview.loadUrl("http://www.google.com"); } } |
I'm a software developer focused on all facets of enterprise solutions and technologies. Currently, I'm enjoying developing iPhone applications at night while spending much of my day working on Java, .Net and database implementations.

#1 by Sam on May 15th, 2010
Chris, thank you for this! Exactly what I needed and super easy to implement like you said. Of all things to keep me stumped, a progress bar!
Thanks again
#2 by Martijn on July 28th, 2010
Thanks Chris, great little tutorials, works like a charm within my surfcheck app!
#3 by nicky on July 28th, 2010
Exactly what i was searching for .. Thanks Chris
#4 by Dorian on August 18th, 2010
Excellent!!!!!!!!
#5 by Namdev on August 25th, 2010
Thanks chris,for the tutorial it work fine. how Can i show the animated progress bar on clicking the link within the webpage.
If i place progressBar.show() in the shouldOverrideUrlLoading(WebView view, String url) method it shows the static progress bar how can i animated it
#6 by Ben on August 27th, 2010
Yeah!!!
thx for the awesome tutorial. I needed to update my dict.cc App
#7 by Ravi on September 7th, 2010
Hi,
In my application’s activity i have one method(like getdata() ) that is calling web services to fetch the data and using the list view to show the data.
Now i want to show a progress bar till when the method is executing as the method takes time to fetch the data.
I have tried many thing but it is not working properly.
if you have any solution plz post it.
Thanks in advance.
#8 by Opensusehack on October 5th, 2010
Hi, thanks for this tutorial
I have a question?
How to autoscale a webview when loadUrl is called????
Thanks
#9 by Samuel on October 21st, 2010
I have posted a solution here : http://stackoverflow.com/questions/3283819/how-do-i-make-my-progress-dialog-dismiss-after-webview-is-loaded/3993002#3993002
#10 by Anup on November 11th, 2010
Hi Chris,
The snippet helped me a lot.
i had a quire,
onPageFinished listener is getting triggered multiple time, during the loading of example “http://www.cnn.com/”
Actually i need to measure the time to Load the page.
as i have kept endTime in onPageFinished listener and startTime before loading url
timeToLoad=endTime -startTime .
Giving wrong results.
Can you help thanks in advance.
#11 by arthur on December 14th, 2010
hey, thank you very much. helped me a lot!
#12 by sam on February 7th, 2011
Hi,
Can please tell me how to add support to javascript in webclient ?not in using the WebChromeClient because I want to use the other methods of webclient
Thank you
#13 by umar on February 28th, 2011
I have tried to implement but everytime i implement it always end up with saying Force to close . can anybody help me out by sending the running project this source code . Thanks in advance
#14 by Mark on April 15th, 2011
Excellent tutorial Chris, was exactly what I was looking for, thanks for posting this.
#15 by Rich on June 26th, 2011
Just wanted to say thanks for this. You’re right, it’s nice and simple, but none of the other demonstrations I came across were quite what I was after. I’ve +1′d this page on Google
#16 by Med on January 13th, 2012
its the best solution i found it its very brilliant idea; thinks a lot