Tuesday, September 2, 2014

WebView With Loading Demo (WebViewClient)

Ø  Create MainActivity.java class in src/package.


MainActivity.java


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

public class MainActivity extends Activity {

       private WebView webView1;
       private ProgressDialog pd;

       @SuppressLint("SetJavaScriptEnabled")
       @Override
       protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);

             pd = ProgressDialog.show(this, "", "Loading...", true);

             webView1 = (WebView) findViewById(R.id.webView1);
             webView1.getSettings().setJavaScriptEnabled(true);
             webView1.getSettings().setSupportZoom(true);
             webView1.getSettings().setBuiltInZoomControls(true);
             webView1.setWebViewClient(new WebViewClient() {
                    @Override
                    public void onPageFinished(WebView view, String url) {
                           if (pd.isShowing() && pd != null) {
                                 pd.dismiss();
                           }
                    }
             });
             webView1.loadUrl("https://www.google.co.in/");
       }
}


Ø  Create main.xml in res/layout.


main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

</LinearLayout>


Ø  Add Permission in AndroidManifest.xml.


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




No comments:

Post a Comment