Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

WebView in Android using Jetpack Compose

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In Android, a WebView is an extension of View Class that allows us to display webpages. In simpler words, if you want to display a webpage in Activity Layout, then you can implement a WebView to display it. It is very similar to a browser but isn’t one. A WebView can rather be referred to as a show or a preview of a browser as it lacks most functionality of that of a browser like a search input, new tabs, incognito, etc. In this article, we will show you how you could implement a WebView in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

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. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Add INTERNET permission in the AndroidManifest.xml file

Navigate to the AndroidManifest.xml file and add INTERNET permission as shown below.


<?xml version="1.0" encoding="utf-8"?>
      <!-- Add this permission -->
    <uses-permission android:name="android.permission.INTERNET"/>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

Step 3: Working with the MainActivity.kt file

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


package com.geeksforgeeks.jcwebview
import android.os.Bundle
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        setContent {
            // Calling the composable function 
            // to display element and its contents
// Creating a composable
// function to display Top Bar
fun MainContent() {
        topBar = { TopAppBar(title = { Text("GFG | WebView", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
        content = { MyContent() }
// Creating a composable 
// function to create WebView
// Calling this function as 
// content in the above function
fun MyContent(){
    // Declare a string that contains a url
    val mUrl = ""
    // Adding a WebView inside AndroidView
    // with layout as full screen
    AndroidView(factory = {
        WebView(it).apply {
            layoutParams = ViewGroup.LayoutParams(
            webViewClient = WebViewClient()
    }, update = {
// For displaying preview in 
// the Android Studio IDE emulator
@Preview(showBackground = true)
fun DefaultPreview() {


When you run the application, you will see that the URL loads in the application as shown below. One thing to make sure of is to keep the device is connected to an active network, or else, the URL won’t load.


My Personal Notes arrow_drop_up
Last Updated : 24 Mar, 2022
Like Article
Save Article
Similar Reads