Search code examples
androidandroid-webview

Issue with maps url opening with webview


I'm using WebView to open maps url (which I get with Google maps share with dialog).

val webView = findViewById<WebView>(R.id.webview)
val webSettings: WebSettings = webView.settings
webSettings.javaScriptEnabled = true
webView.webViewClient = WebViewClient()
val url = "https://maps.app.goo.gl/ei5DupR88SYBF8Am8"
webView.loadUrl(url)

But url is not opened like any other url.

It looks like, "https://" is replaced with "intent://" and WebView cannot open invalid url.

Any idea why that happens and how to avoid it?

enter image description here


Solution

  • I solved this by overriding "shouldOverrideUrlLoading":

    companion object {
        const val FALLBACK_URL = "browser_fallback_url="
    }
    
    
    webView.webViewClient = object: WebViewClient() {
        override fun shouldOverrideUrlLoading(webView: WebView?, request: WebResourceRequest):
                Boolean {
    
            val url = request.url
            val urlString = url.toString()
            val fallbackUrl = urlString.subSequence(
                    urlString.indexOf(FALLBACK_URL) + FALLBACK_URL.length,
                    urlString.length)
                    .toString()
            webView.loadUrl(URLDecoder.decode(fallbackUrl, "UTF-8"))
            return true
        }
    }