Introduction

In this article, you will learn about WebView in Android.
WebView
A WebView is used to display web pages or to display some online content in your activity. It can either display remote web pages or load static HTML data. For this, you need to give permission to the Android menifest.xml file. For that, we need to first use the getsettings() method to get the object that handles settings of the WebView. You need to set the setJavaScriptEnables() to true to enable the JavaScript because JavaScript is disabled for the WebView.
Step 1
Create an XML file and write this:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingLeft="@dimen/activity_horizontal_margin"
  6. android:paddingRight="@dimen/activity_horizontal_margin"
  7. android:paddingTop="@dimen/activity_vertical_margin"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity"
  10. android:background="#81a3d0">
  11. <Button
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:id="@+id/button"
  15. android:text="www.google.com"/>
  16. </RelativeLayout>
Step 2
Create another XML file with the following:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <WebView
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:id="@+id/webView"
  10. android:layout_gravity="center">
  11. </WebView>
  12. </LinearLayout>
Step 3
Create a Java class file and write this:
  1. package com.webviewexample;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. public class MainActivity extends Activity {
  8. Button button;
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. button=(Button)findViewById(R.id.button);
  14. button.setOnClickListener(new View.OnClickListener() {
  15. @Override
  16. public void onClick(View view) {
  17. Intent i=new Intent(MainActivity.this,Web.class);
  18. startActivity(i);
  19. }
  20. });
  21. }
  22. }
Step 4
Create another Java class and write this:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingLeft="@dimen/activity_horizontal_margin"
  6. android:paddingRight="@dimen/activity_horizontal_margin"
  7. android:paddingTop="@dimen/activity_vertical_margin"
  8. android:paddingBottom="@dimen/activity_vertical_margin"
  9. tools:context=".MainActivity"
  10. android:background="#81a3d0">
  11. <Button
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:id="@+id/button"
  15. android:text="www.google.com"/>
  16. </RelativeLayout>
Step 5
Change the Android menifest,xml file so that it is like this:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.webviewexample"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="16" />
  9. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  10. <application
  11. android:allowBackup="true"
  12. android:icon="@drawable/ic_launcher"
  13. android:label="@string/app_name"
  14. android:theme="@style/AppTheme" >
  15. <activity
  16. android:name="com.webviewexample.MainActivity"
  17. android:label="@string/app_name" >
  18. <intent-filter>
  19. <action android:name="android.intent.action.MAIN" />
  20. <category android:name="android.intent.category.LAUNCHER" />
  21. </intent-filter>
  22. </activity>
  23. <activity
  24. android:name=".Web">
  25. </activity>
  26. </application>
  27. </manifest>
Step 6
Clipboard04.jpg
Step 7
Clipboard02.jpg