Load WebView with ProgressDialog
|
|
It’s very useful to determine the status, while loading a webpage using WebView. So in this tutorial we will learn about the same. If you are already not familiar with the WebView then you can checkout our previous tutorial about WebView.
Here is our step by step tutorial-
1.
Create a new project by going to File ⇒ New Android Project and fill required details:
Project Name : WebViewWithProgressDialog
Package : com.sunil.wvpd
Main Activity : Main.java
Build Target : Android 2.2
2. Since we’re going to load a URL in the WebView widget, we have to make
sure to request permission to access the Internet in
AndroidManifest.xml. Open AndroidManifest.xml file and declare a
uses-permission element just above <application> ...
</application> element.
<uses-permission android:name="android.permission.INTERNET"/>
3. Now open the Main.java file and add the following code:
package com.sunil.wvpd;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Main extends Activity {
WebView mWeb;
ProgressDialog mProgress;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide Titlebar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set webview as main content only
mWeb = new WebView(this);
setContentView(mWeb);
// set Javascript
WebSettings settings = mWeb.getSettings();
settings.setJavaScriptEnabled(true);
// the init state of progress dialog
mProgress = ProgressDialog.show(this, "Loading", "Please wait for a moment...");
// Add a WebViewClient, which actually handles loading data from web
mWeb.setWebViewClient(new WebViewClient() {
// Load the url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
// When finish loading page
public void onPageFinished(WebView view, String url) {
if(mProgress.isShowing()) {
mProgress.dismiss();
}
}
});
// Set url for webview to load
mWeb.loadUrl("http://www.androidaspect.com/");
}
}
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class Main extends Activity {
WebView mWeb;
ProgressDialog mProgress;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hide Titlebar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Set webview as main content only
mWeb = new WebView(this);
setContentView(mWeb);
// set Javascript
WebSettings settings = mWeb.getSettings();
settings.setJavaScriptEnabled(true);
// the init state of progress dialog
mProgress = ProgressDialog.show(this, "Loading", "Please wait for a moment...");
// Add a WebViewClient, which actually handles loading data from web
mWeb.setWebViewClient(new WebViewClient() {
// Load the url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
// When finish loading page
public void onPageFinished(WebView view, String url) {
if(mProgress.isShowing()) {
mProgress.dismiss();
}
}
});
// Set url for webview to load
mWeb.loadUrl("http://www.androidaspect.com/");
}
}
4. Finally run your project.









Post a Comment