Kotlin Android WebView
Android WebView is a View component that displays web content inside an Activity. It lets an application open a web page without switching to a separate browser app.
You can place a WebView in an XML layout like a TextView, Button, or another Android View. The application can then load a remote URL, local HTML, or an HTML string into it.
Add Internet Permission for Android WebView
Step 1: Add the INTERNET permission to AndroidManifest.xml when the WebView loads a page from the internet. This is a normal permission, so the application does not need to request it at runtime.
<uses-permission android:name="android.permission.INTERNET" />
Include WebView in the Android XML Layout
Step 2: Add a WebView element to the Activity layout. Assign it an ID so that the Activity can obtain a reference to it.
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Load a URL in WebView with Kotlin
Step 3: Call loadUrl() after obtaining the WebView from the layout.
webView.loadUrl("https://www.google.com/")
Without a WebViewClient, links selected on the loaded page may be handed to another application. Assigning a WebViewClient keeps normal page navigation within the WebView.
webView.webViewClient = WebViewClient()
webView.loadUrl("https://www.google.com/")
Complete Kotlin Android WebView Example
In this Kotlin Android application, a WebView is added to the Activity layout and used to load the Google website.
Create a Kotlin Android Application with Empty Activity and modify the following files.
AndroidManifest.xml
The manifest declares the internet permission required to retrieve the remote page.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutorialkart.webviewexample">
<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/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
The layout contains a heading followed by a WebView that fills the remaining available area.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEE"
tools:context=".MainActivity">
<TextView
android:text="Web View Example"
android:gravity="center"
android:padding="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Original MainActivity.kt Example
The following code is the original example, which uses Android synthetic view access. Synthetic view access is no longer used in current Android projects; a current alternative is shown immediately after it.
package com.tutorialkart.webviewexample
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView.loadUrl("https://www.google.com/")
}
}
Current MainActivity.kt Using findViewById
For a current Android project, use AndroidX AppCompatActivity and obtain the WebView with findViewById(). Set the WebViewClient before loading the URL so that selected links remain inside the application.
package com.tutorialkart.webviewexample
import android.os.Bundle
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var webView: WebView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
webView = findViewById(R.id.webView)
webView.webViewClient = WebViewClient()
webView.loadUrl("https://www.google.com/")
}
override fun onDestroy() {
webView.destroy()
super.onDestroy()
}
}
Run the Android application. The requested page appears inside the WebView, as shown in the following screenshot.

Enable JavaScript in Android WebView When Required
JavaScript is disabled in WebView by default. Enable it only when the content being displayed requires it and the application trusts that content.
webView.settings.javaScriptEnabled = true
Avoid enabling unnecessary WebView capabilities. In particular, do not expose application objects through a JavaScript interface unless the loaded content and navigation are strictly controlled.
Handle the Android Back Button in WebView
When a user has followed links inside the WebView, the Back button should normally return to the previous web page before closing the Activity. Current Android projects can implement this behavior with an OnBackPressedCallback.
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (webView.canGoBack()) {
webView.goBack()
} else {
isEnabled = false
onBackPressedDispatcher.onBackPressed()
}
}
})
Add import androidx.activity.OnBackPressedCallback to the Activity when using this callback.
Load HTML Content Directly in WebView
Use loadDataWithBaseURL() when the application already has an HTML string. Supplying UTF-8 encoding helps preserve non-ASCII characters.
val html = """
<html>
<body>
<h2>WebView Content</h2>
<p>This HTML was supplied by the Android application.</p>
</body>
</html>
""".trimIndent()
webView.loadDataWithBaseURL(
null,
html,
"text/html",
"UTF-8",
null
)
Why an Android WebView Page Does Not Load
- Missing internet permission: Confirm that
android.permission.INTERNETappears directly inside the manifest element. - HTTP URL blocked: Current Android configurations commonly restrict cleartext HTTP traffic. Use HTTPS whenever the website supports it instead of weakening the application’s network security settings.
- JavaScript-dependent page: If a trusted page requires JavaScript, enable it through
webView.settings.javaScriptEnabled. - Links open outside the app: Assign a
WebViewClientto the WebView. - Server or certificate problem: Test the same HTTPS URL in a browser and fix invalid TLS certificates rather than bypassing certificate errors in the application.
- Network unavailable: Confirm that the emulator or physical device has a working connection.
Android WebView Security and Lifecycle Checks
- Load HTTPS pages and restrict navigation when the WebView should access only approved domains.
- Enable JavaScript only when the page requires it.
- Do not ignore SSL errors in
onReceivedSslError(). - Validate untrusted URLs before passing them to
loadUrl(). - Avoid granting file, camera, microphone, or location access unless the feature requires it and the user understands the request.
- Destroy a WebView that is no longer needed to release its resources.
Kotlin Android WebView FAQs
How do I open a URL inside a WebView in Kotlin?
Obtain the WebView from the layout, assign a WebViewClient, and call webView.loadUrl("https://example.com"). Add the internet permission to the manifest for remote pages.
Why does a WebView URL open in the external browser?
This usually happens when the WebView does not have a WebViewClient. Set webView.webViewClient = WebViewClient() before loading the page.
Does Android WebView require internet permission?
Internet permission is required when the WebView retrieves remote content. It is not required merely to display an HTML string already stored in the application.
Should JavaScript always be enabled in WebView?
No. Leave JavaScript disabled unless the displayed page needs it. If it must be enabled, load controlled or trusted content and avoid exposing unnecessary application interfaces.
Kotlin Android WebView Tutorial QA Checklist
- The manifest contains the internet permission for remote URLs.
- The XML WebView ID matches the ID referenced by the Kotlin Activity.
- A WebViewClient is assigned when navigation should remain inside the app.
- The example URL uses HTTPS.
- JavaScript is enabled only if the selected page requires it.
- Back navigation is tested after opening multiple pages in the WebView.
- SSL errors, offline behavior, and unavailable pages are handled without bypassing security checks.
Kotlin Android WebView Summary
In this Kotlin Android Tutorial, we learned how to add a WebView to an XML layout, load a URL with Kotlin, keep links inside the application, display an HTML string, support WebView history, and diagnose common loading problems.
TutorialKart.com