Open In App

How to Use WebView in Android?

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

WebView is a view that displays web pages inside the application. It is used to turn the application into a web application.

public class WebView extends AbsoluteLayout implements 
    ViewTreeObserver.OnGlobalFocusChangeListener, 
    ViewGroup.OnHierarchyChangeListener

Class Hierarchy:

java.lang.Object
   ↳  android.view.View
        ↳  android.view.ViewGroup
             ↳  android.widget.AbsoluteLayout
                  ↳  android.webkit.WebView

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Working with the MainActivity File

Go to the MainActivity File and refer to the following code. Below is the code for the MainActivity File. Comments are added inside the code to understand the code in more detail.

Java




import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // Find the WebView by its unique ID
        WebView webView = findViewById(R.id.web);
 
        // loading https://www.geeksforgeeks.org url in the WebView.
        webView.loadUrl("https://www.geeksforgeeks.org");
 
        // this will enable the javascript.
        webView.getSettings().setJavaScriptEnabled(true);
 
        // WebViewClient allows you to handle
        // onPageFinished and override Url loading.
        webView.setWebViewClient(new WebViewClient());
    }
}


Kotlin




import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
 
class MainActivity : AppCompatActivity() {
     
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
 
        // Find the WebView by its unique ID
        val webView = findViewById<WebView>(R.id.web)
 
        // loading http://www.google.com url in the WebView.
        webView.loadUrl("https://www.geeksforgeeks.org")
 
        // this will enable the javascript.
        webView.settings.javaScriptEnabled = true
 
        // WebViewClient allows you to handle
        // onPageFinished and override Url loading.
        webView.webViewClient = WebViewClient()
    }
}


Step 3: Working with the XML Files

Next, go to the activity_main.xml file, which represents the UI of the project. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.gfg.android.examplekotlin.MainActivity">
 
    <!-- unique ID of WebView -->
    <WebView
        android:id="@+id/web"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />
</RelativeLayout>


Step 4: Adding Permissions to the AndroidManifest.xml File

In AndroidManifest.xml, one needs to include the below permission, in order to access the internet

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

Output:

WebView in Android

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads