Android Webview - Open Multiple file in one webview

Open different files in one Webview


activity_main2.xml

 <WebView

    android:id="@+id/webViewOffline"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

MainActivity2.kt

// Write this code inside onCreate
// Set up one webview multiple file open
val filename = intent.getStringExtra("filename")

val webView: WebView = findViewById(R.id.webViewOffline)
webView.webViewClient = WebViewClient()
webView.settings.javaScriptEnabled = true

loadHtmlFile(webView, filename ?: "index.html")

//Function - Out of onCreate
private fun loadHtmlFile(webView: WebView, filename: String) {
val path = "file:///android_asset/$filename"
webView.loadUrl(path)
}






activity_main.xml:

Create buttons with ids on activity_main.xml





MainActivity.kt


//Wtite this code from where you want to load the files with buttons
//buttons
val button1 = findViewById<Button>(R.id.button1)
val button2 = findViewById<Button>(R.id.button2)
val button3 = findViewById<Button>(R.id.button3)

// Handle button clicks
button1.setOnClickListener { openWebViewActivity("location.html") }
// Add click listeners for Button 2 to Button 6
button2.setOnClickListener { openWebViewActivity("info.html") }
button3.setOnClickListener { openWebViewActivity("index.html") }




Add Permission on Manifest

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














Comments