Problem loading URL with Webview Kotlin

Asked

Viewed 23 times

-1

I’m trying to open an html inside the Assets in Android Studio, but the page is completely blank.

NOTE: When I put any external link, it works.

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(com.example.myapplication0.R.layout.activity_main)


    val webView = findViewById<WebView>(com.example.myapplication0.R.id.webview)

    val webSetting = webView.settings
    webSetting.builtInZoomControls = true
    webView.setWebViewClient(WebViewClient())

    webView.loadUrl("assets/index.html")







}

}

This is my XML

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

1 answer

0

You need to point to the absolute path of your Assets folders.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(com.example.myapplication0.R.layout.activity_main)


    val webView = findViewById<WebView>(com.example.myapplication0.R.id.webview)

    val webSetting = webView.settings
    webSetting.builtInZoomControls = true
    webView.setWebViewClient(WebViewClient())

    webView.loadUrl("file:///android_asset/index.html")
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.