WebView in Android using Jetpack Compose
Last Updated :
24 Mar, 2022
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
<? xml version = "1.0" encoding = "utf-8" ?>
package = "com.geeksforgeeks.jcwebview" >
< uses-permission android:name = "android.permission.INTERNET" />
< application
android:allowBackup = "true"
android:icon = "@mipmap/ic_launcher"
android:label = "@string/app_name"
android:roundIcon = "@mipmap/ic_launcher_round"
android:supportsRtl = "true"
android:theme = "@style/Theme.JCWebView" >
< activity
android:name = ".MainActivity"
android:exported = "true"
android:label = "@string/app_name"
android:theme = "@style/Theme.JCWebView" >
< intent-filter >
< action android:name = "android.intent.action.MAIN" />
< category android:name = "android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
</ application >
</ manifest >
|
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.
Kotlin
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.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super .onCreate(savedInstanceState)
setContent {
MainContent()
}
}
}
@Composable
fun MainContent() {
Scaffold(
topBar = { TopAppBar(title = { Text( "GFG | WebView" , color = Color.White) }, backgroundColor = Color( 0xff0f9d58 )) },
content = { MyContent() }
)
}
@Composable
fun MyContent(){
AndroidView(factory = {
WebView(it).apply {
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
webViewClient = WebViewClient()
loadUrl(mUrl)
}
}, update = {
it.loadUrl(mUrl)
})
}
@Preview (showBackground = true )
@Composable
fun DefaultPreview() {
MainContent()
}
|
Output:
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...