How to Use WebView in Android Studio

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.    
  12.     <Button  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/button"  
  16.             android:text="www.google.com"/>  
  17.    
  18. </RelativeLayout> 
Step 2
 
Create another XML file with the following:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.    
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.               android:orientation="vertical"  
  5.               android:layout_width="match_parent"  
  6.               android:layout_height="match_parent">  
  7.    
  8.     <WebView  
  9.             android:layout_width="wrap_content"  
  10.             android:layout_height="wrap_content"  
  11.             android:id="@+id/webView"  
  12.             android:layout_gravity="center">  
  13.    
  14.             </WebView>  
  15.    
  16. </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.    
  8. public class MainActivity extends Activity {  
  9.    
  10. Button button;  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.    
  16.         button=(Button)findViewById(R.id.button);  
  17.         button.setOnClickListener(new View.OnClickListener() {  
  18.             @Override  
  19.             public void onClick(View view) {  
  20.                 Intent i=new Intent(MainActivity.this,Web.class);  
  21.          startActivity(i);  
  22.             }  
  23.         });  
  24.     }  

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.    
  12.     <Button  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/button"  
  16.             android:text="www.google.com"/>  
  17.    
  18. </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.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <uses-permission android:name="android.permission.INTERNET"></uses-permission>  
  12.    
  13.     <application  
  14.         android:allowBackup="true"  
  15.         android:icon="@drawable/ic_launcher"  
  16.         android:label="@string/app_name"  
  17.         android:theme="@style/AppTheme" >  
  18.         <activity  
  19.             android:name="com.webviewexample.MainActivity"  
  20.             android:label="@string/app_name" >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.    
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.         <activity  
  28.                 android:name=".Web">  
  29.    
  30.         </activity>  
  31.     </application>  
  32. </manifest> 
Step 6
 
Clipboard04.jpg
 
Step 7
 
Clipboard02.jpg


Similar Articles