Android WebView




Android WebView


  1. Static WebView Creation in Android
  2. Dynamic WebView Creation in Android


Static WebView Creation in Android


Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

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

    <EditText android:id="@+id/URL"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="www.google.com"/>

    <Button android:id="@+id/Go"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Go"/>

    <WebView android:id="@+id/WebEngine"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>

Step 3 : Open src -> package -> MainActivity.java and add following code :


package com.gudivada.hemanthsomaraju;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

/** Called when the activity is first created. */

 EditText URL;
 Button Go;
 WebView Browser;

 private class WebClient extends WebViewClient
 {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
 }

  @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

URL = (EditText)findViewById(R.id.URL);
Go = (Button)findViewById(R.id.Go);
Browser = (WebView) findViewById(R.id.WebEngine);
Browser.loadUrl("http://www.google.co.in/");
   
         Go.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Browser.setWebViewClient(new WebClient());
Browser.loadUrl("http://"+URL.getText().toString());
}
});
}
 }

Step 4 : Open AndroidManifest.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gudivada.hemanthsomaraju"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 5 : Our output will be like this :










Dynamic WebView Creation in Android

Step 1 : Select File -> New -> Project -> Android Application Project (or) Android Project. Fill the forms and click "Finish" button. If you have any doubt regarding create a new project Click Here.

Step 2 : Open res -> layout -> activity_main.xml (or) main.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/linear"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
         
    <EditText android:id="@+id/URL"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="www.gmail.com"/>

    <Button android:id="@+id/Go"  
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Go"/>

</LinearLayout>

Step 3 : Open src -> package -> MainActivity.java and add following code :

package com.gudivada.hemanthsomaraju;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

LinearLayout linear;
EditText URL;
Button Go;
WebView Browser;

private class WebClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
   
        linear=(LinearLayout) findViewById(R.id.linear);
        URL=(EditText) findViewById(R.id.URL);
        Go=(Button) findViewById(R.id.Go);
        Browser=new WebView(MainActivity.this);
        linear.addView(Browser);
Browser.setWebViewClient(new WebClient());

        Browser.loadUrl("http://www.gmail.com");

        Go.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Browser.setWebViewClient(new WebClient());
Browser.loadUrl("http://"+URL.getText().toString());
}
});    
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

Step 4 : Open AndroidManifest.xml and add following code :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gudivada.hemanthsomaraju"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.gudivada.hemanthsomaraju.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Step 5 : Our output will be like this :