kotlinwebviewandroid-jetpack-compose

Kotlin WebView loadUrl converts https address to intent causing ERR_UNKNOWN_URL_SCHEME. I am NOT using any "mailto", "tel", etc


WebView loadUrl converts https address to intent causing ERR_UNKNOWN_URL_SCHEME. I am NOT using any "mailto", "tel", etc.

I am using a WebView in Compose. Running in Android Studio emulator.

Using the same address in my computer it works correctly. Using the same address in the Emulator does not work correctly.

[http://maps.google.com/maps?z=16&t=h&q=loc:18.2569533+-66.006805&hl=en]

This is the error:

WebView generated error

This is my function:

@OptIn(ExperimentalComposeUiApi::class)
@SuppressLint("SetJavaScriptEnabled")
@Composable
fun WebViewScreen(location: LatLng){ 

val url = "http://maps.google.com/maps?z=16&t=h&q=loc:${location.latitude}+${location.longitude}&hl=${Locale.getDefault().language}"

AndroidView(
    modifier = Modifier
        .pointerInteropFilter {
        when (it.action) {
            MotionEvent.ACTION_DOWN -> {}
            MotionEvent.ACTION_MOVE -> {}
            MotionEvent.ACTION_UP -> {}
            else ->  false
        }
        true
    },
    factory = { context ->
        WebView(context).apply {
            layoutParams = ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
            settings.javaScriptEnabled = true
            webViewClient = WebViewClient()

            settings.loadWithOverviewMode = true
            settings.useWideViewPort = true
            settings.setSupportZoom(true)

            loadUrl(url)
        }
    },
    update = { webView ->
        webView.loadUrl(url)
    }
)
}

Solution

  • The problem was caused by the Emulator. I updated the Android Emulator in the SDK Manager.

    You have to recreate the Emulator for the update to take effect so backup the Data on your Bad Emulator to copy it to the new Emulator.

    I found another solution but did not work for me. Maybe I had to recreate the emulator anyway to make it work.