Many enterprise applications combine high-performance native screens with web-powered content — such as rich checkout flows, interactive dashboards, terms of service, and existing React/Vue widgets. Android WebView provides an embedded Chromium browser engine inside your application, accompanied by a powerful two-way JavaScript Bridge.
Figure 35.1: Web to Native Integration — Option A: Embedded WebView with JavascriptInterface Bridge; Option B: Fullscreen Trusted Web Activity (TWA).
window.AndroidBridge.someMethod(), invoking a Kotlin method annotated with @JavascriptInterface.webView.evaluateJavascript("myJsFunction(data)", callback) to trigger web functions or inject data directly into the DOM.import android.content.Context
import android.webkit.JavascriptInterface
import android.widget.Toast
import android.os.Vibrator
import android.os.VibrationEffect
import android.os.Build
class WebAppInterface(private val context: Context) {
// Must annotate with @JavascriptInterface for security!
@JavascriptInterface
fun showToast(toastMessage: String) {
Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show()
}
@JavascriptInterface
fun triggerHapticFeedback() {
val vibrator = context.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
vibrator.vibrate(VibrationEffect.createOneShot(50, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
vibrator.vibrate(50)
}
}
@JavascriptInterface
fun getAppVersion(): String {
return "2026.1.0-PROD"
}
}
import android.webkit.WebView
import android.webkit.WebViewClient
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import androidx.compose.runtime.Composable
import androidx.compose.ui.viewinterop.AndroidView
@Composable
fun HybridWebScreen(url: String) {
AndroidView(
factory = { context ->
WebView(context).apply {
settings.apply {
javaScriptEnabled = true // Enable JS
domStorageEnabled = true // Enable localStorage
databaseEnabled = true
// Security best practices:
allowFileAccess = false
allowContentAccess = false
}
webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
// Example: Call web JavaScript function from Kotlin
view?.evaluateJavascript("window.updateTheme('dark')", null)
}
}
webChromeClient = WebChromeClient()
// Expose the bridge interface to window.AndroidBridge
addJavascriptInterface(WebAppInterface(context), "AndroidBridge")
loadUrl(url)
}
}
)
}
<!DOCTYPE html>
<html>
<head>
<title>Veewom Hybrid Web Portal</title>
</head>
<body>
<h2>Web to Native Integration</h2>
<button onclick="callNative()">Vibrate Device from Web!</button>
<script>
function callNative() {
// Verify bridge is present before calling
if (window.AndroidBridge) {
window.AndroidBridge.triggerHapticFeedback();
window.AndroidBridge.showToast("Triggered directly from Web JavaScript!");
} else {
console.log("Not running inside Android native WebView.");
}
}
// Called from Kotlin onPageFinished:
window.updateTheme = function(mode) {
document.body.style.backgroundColor = (mode === 'dark') ? '#04121F' : '#FFFFFF';
};
</script>
</body>
</html>
Never add a @JavascriptInterface to a WebView that loads untrusted, third-party public web URLs! Malicious third-party scripts could invoke your native methods to access user hardware or execute arbitrary code. Only attach bridge interfaces to local trusted HTML assets or verified HTTPS domains you control.
Broaden your foundational and advanced Android application development skills across our curriculum:
Compare embedded WebView bridge with full-screen TWA PWA packaging.
Intercept web URLs and redirect them to native app screens.
Bridge WebAuthn passkey requests between web views and native biometric prompt.
Prevent cross-site scripting (XSS) and disable file access in WebView.