Friday 13 December 2013

how to load a url in webview with ProgressDialog in android

This Blog describe the android tutorials which contain android code on different widgets, updates of android market, download project option. it is created and managed by a Android apps developer Specialist who works in www.itdevelopersdelhi.in

In this tutorial i am going to describe how to load a URL in web view in android with ProgressDialog in this the web page will be loaded when the background loading is completed and progress dialog will be get disappear.

Source code :-

import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
   
    WebView myWebView;
    ProgressDialog mProgressDialog;
    String myAddr = "http://itdevelopersdelhi.in/";
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        myWebView=(WebView)findViewById(R.id.webView1);
        mProgressDialog = new ProgressDialog(MainActivity.this);
       
        mProgressDialog.setTitle("Android Tutorial");
        mProgressDialog.setCanceledOnTouchOutside(false);
        mProgressDialog.show();
       
        myWebView.getSettings().setUseWideViewPort(true);
        myWebView.getSettings().setLoadWithOverviewMode(true);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.loadUrl(myAddr);
        myWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
                if (mProgressDialog.isShowing()) {
                    mProgressDialog.dismiss();
                }
            }
        });
    }
}


XML Code :-

<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"
    tools:context=".MainActivity" >

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

</RelativeLayout>

Snapshot :-



 

No comments:

Post a Comment