WebView with Progress Dialog

WebView with Progress Dialog
In Menifest.xml add INTERNET Permission

MainActivity.java


import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {
WebView web;
ProgressDialog dialog;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Hide Title Bar - This Should be done before SetContentView

this.requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_main);
// Inflation

web = (WebView) findViewById(R.id.webview);
web.setWebViewClient(new WebViewClient() {

// This method will be triggered when the Page Started Loading

public void onPageStarted(WebView view, String url, Bitmap favicon) {
dialog = ProgressDialog.show(MainActivity.this, null,
"Please Wait...Page is Loading...");
dialog.setCancelable(true);
super.onPageStarted(view, url, favicon);
}

// This method will be triggered when the Page loading is completed

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

// This method will be triggered when error page appear

public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
dialog.dismiss();
// You can redirect to your own page instead getting the default
// error page
Toast.makeText(ProgressDemoActivity.this,
"The Requested Page Does Not Exist", Toast.LENGTH_LONG).show();
web.loadUrl("http://codescoding.blogspot.com");
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
web.loadUrl("
http://codescoding.blogspot.com");
web.getSettings().setLoadWithOverviewMode(true);
web.getSettings().setUseWideViewPort(true);
}
}
activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
I Am Not The Owner Of These Code .I Merely Have Copied Them From Various Sources. The Only Thing I Did Is That I Am Going To Present Them In More Easy Way To Understand.

Comments

Popular posts from this blog

LED Blinking using 8051 Microcontroller and Keil C – AT89C51

Android Camera Example 2

Java Script to make text change text color