I want to click on terms in a android web view to do an action, like showing a toast that contains that term that we already clicked.
`class WebAppInterface(private val context: Context) {
@JavascriptInterface
fun showToast(toastMessage: String) {
Toast.makeText(context, toastMessage, Toast.LENGTH_SHORT).show()
}
}
@SuppressLint("SetJavaScriptEnabled")
@Composable
fun HtmlText(htmlContent: String) {
val rightAlignedHtmlContent = """
<html>
<head>
<style>
body {
text-align: right;
}
</style>
</head>
<body>
$htmlContent
</body>
</html>
""".trimIndent()
Column(
modifier = Modifier
.fillMaxSize()
.background(color = Color.Transparent, shape = RoundedCornerShape(16.dp))
.border(width = 1.dp, color = Color.Transparent),
) {
AndroidView(
modifier = Modifier
.fillMaxSize().padding(8.dp)
.background(color = Color.Transparent, shape = RoundedCornerShape(16.dp))
.border(width = 1.dp, color = Color.Transparent),
factory = { context ->
WebView(context).apply {
webViewClient = WebViewClient()
settings.javaScriptEnabled = true
addJavascriptInterface(WebAppInterface(context), "Android")
loadDataWithBaseURL(null, rightAlignedHtmlContent, "text/html", "UTF-8", null)
}
},
update = { webView ->
webView.loadDataWithBaseURL(null, rightAlignedHtmlContent, "text/html", "UTF-8", null)
}
)
}
}`
This is my jetpack compose code.
the click on each term in AndroidView is not working.