Monday, 7 July 2014

how to create a webview in android example

MainActivity.java

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity
{
   
    private WebView webView;
     @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
       
        webView = (WebView) findViewById(R.id.webView1);
    
            startWebView("your url");       
    }
   
    private void startWebView(String url)
    {
       
        webView.setWebViewClient(new WebViewClient()
        {     
         ProgressDialog progressDialog;
         
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {             
                view.loadUrl(url);
                return true;
            }
       
            public void onLoadResource (WebView view, String url)
            {
                if (progressDialog == null)
                {
                    progressDialog = new ProgressDialog(MainActivity.this);
                    progressDialog.setMessage("Loading...");
                    progressDialog.show();
                }
            }
            public void onPageFinished(WebView view, String url)
            {
                try
                {
                if (progressDialog.isShowing())
                {
                    progressDialog.dismiss();
                    progressDialog = null;
                }
                }catch(Exception exception)
                {
                    exception.printStackTrace();
                }
            }
            
        });
       
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.setWebChromeClient(new WebChromeClient());
       
        webView.loadUrl(url);
         
    }
  }

AndroidManifest.xml

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

activity_main.xml

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

    <RelativeLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true" >

        <WebView
            android:id="@+id/webView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="20dp" />
    </RelativeLayout>

</RelativeLayout>

No comments:

Post a Comment