Android webview content not loaded for hyperlink consists with hash

Viewed 186

I am unable to load content in webview which has data with a hyperlink that comes with the '#' hash code. like below.

<a href="https://safetravel.changiairport.com/#/">here</a>

Full web content is here.

I am loading webview like this way.

binding.webView.loadData(
            getHtmlData(viewModel.blogPost.get()!!.formatContentData()),
            "text/html",
            "UTF-8")

@Parcelize
data class BlogPost(
        val title: String,
        ... remove for brebity 
    

) : Parcelable {

    fun formatContentData(): String {
        val justifyTag = "<html><body style='text-align:justify;'>%s</body></html>"
        content?.let {

            var data = it.replace("&lt;", "<").replace("&gt;", ">")

            val tagList = arrayOf("flightnode", "hotelnode ", "packagenode")

            for (tag in tagList) {
                val patternString = "(?is)<$tag[^>]+>.*?<\\/$tag>"
                val pattern = Pattern.compile(patternString)
                val matcher = pattern.matcher(data)
                println("\n\n" + matcher.find())
                data = matcher.replaceAll("")
            }

            return java.lang.String.format(Locale.US, justifyTag, data)
        }
        return ""
    }
}

Any help is appriciated.

1 Answers

Try using the loadDataWithBaseURL method instead.

Just make sure to use a valid baseURL. I used "app:htmlPage24" like in this answer: https://stackoverflow.com/a/7264081/11869925

webView.loadDataWithBaseURL("app:htmlPage24", content, "text/html; charset=utf-8", "UTF-8", null)
Related