Implementing on Webview

    Implementing on Webview


    Article summary

    Refer to the following information when using a WebView in your Android app:

    WebViewClient

    shouldOverrideUrlLoading

    Do not put webView.loadUrl(url) within the shouldOverrideUrlLoading function. If there is a separate scheme, refer to the following code and define it within that function.

    webView.webViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?
        ): Boolean {
            // if you have custom scheme
            val url = request?.url ?: return super.shouldOverrideUrlLoading(view, request)
            if (url.toString().startsWith("market")) {
                val intent = Intent(Intent.ACTION_VIEW).apply {
                    data = url
                }
                startActivity(intent)
                return true
            }
            return super.shouldOverrideUrlLoading(view, request)
        }
    }


    WebChromeClient

    onCreateWindow

    1. If you want to switch windows when clicking on a product, refer to the following code.

    webView.webChromeClient = object : WebChromeClient() {
        override fun onCreateWindow(
            view: WebView?,
            isDialog: Boolean,
            isUserGesture: Boolean,
            resultMsg: Message?
        ): Boolean {
            view ?: return true
            resultMsg ?: return true
            val subWebView = WebView(view.context)
            subWebView.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                    url ?: return false
                    if (url.startsWith("http")) {
                        // if you want to open current webView
                        webView.loadUrl(url)
                    }
                    return true
                }
                override fun shouldOverrideUrlLoading(
                    view: WebView?,
                    request: WebResourceRequest?
                ): Boolean {
                    val url = request?.url?.toString() ?: return false
                    if (url.startsWith("http")) {
                        // if you want to open current webView
                        webView.loadUrl(url)
                    }
                    return true
                }
            }
            val viewTransport = (resultMsg.obj as? WebView.WebViewTransport) ?: return true
            viewTransport.webView = subWebView
            resultMsg.sendToTarget()
            return true
        }
    }

    2. If you want to open a new window when clicking on a product, refer to the following code.

    webView.webChromeClient = object : WebChromeClient() {
        override fun onCreateWindow(
            view: WebView?,
            isDialog: Boolean,
            isUserGesture: Boolean,
            resultMsg: Message?
        ): Boolean {
            view ?: return true
            resultMsg ?: return true
            val subWebView = WebView(view.context)
            subWebView.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean {
                    url ?: return false
                    if (url.startsWith("http")) {
                        // if you want to open new page
                        val intent = Intent(Intent.ACTION_VIEW).apply {
                            data = Uri.parse(url)
                        }
                        startActivity(intent)
                    }
                    return true
                }
                override fun shouldOverrideUrlLoading(
                    view: WebView?,
                    request: WebResourceRequest?
                ): Boolean {
                    val url = request?.url?.toString() ?: return false
                    if (url.startsWith("http")) {
                        // if you want to open new page
                        val intent = Intent(Intent.ACTION_VIEW).apply {
                            data = Uri.parse(url)
                        }
                        startActivity(intent)
                    }
                    return true
                }
            }
            val viewTransport = (resultMsg.obj as? WebView.WebViewTransport) ?: return true
            viewTransport.webView = subWebView
            resultMsg.sendToTarget()
            return true
        }
    }


    What's Next