This is a composable function and I want to do that when I click on cardview then article will open in android view but didn't understand how to achieve this. I also try with lamda but not got success.
I Want to call ShowOnWebView composable function when NewsCardView(Card View) function clicked but compiler shows an error "@Composable invocations can only happen from the context of a @Composable function" how to call my function when cardview will clicked
Kindly help and thanks in andvance
@Composable
fun NewsCardView(
article: Article,
modifier: Modifier = Modifier,
) {
Card(
modifier = modifier
.height(150.dp)
.fillMaxWidth()
.padding(2.dp)
.clickable {
// I Want to call ShowOnWebView composable function but compiler
shows an error "@Composable invocations can only happen from the context of a
@Composable function"
how to call my function when cardview will clicked
},
elevation = 5.dp,
backgroundColor = Color.White
) {
val painter = rememberImagePainter(data = article.urlToImage) {
crossfade(1000)
error(R.drawable.ic_placeholder)
placeholder(R.drawable.ic_placeholder)
}
Row(
modifier = Modifier
.fillMaxSize(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceEvenly
) {
Column(
modifier = Modifier.fillMaxWidth(.3f),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Image(
modifier = Modifier.size(100.dp),
painter = painter,
contentDescription = "News",
contentScale = ContentScale.Crop,
)
Text(
text = article.author ?: "",
fontSize = 12.sp,
)
Text(
text = article.publishedAt,
fontSize = 12.sp,
)
}
Column(
modifier = Modifier.fillMaxWidth(.7f),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(modifier = Modifier.fillMaxWidth(),
text = article.title,
color = Color.Black,
fontWeight = FontWeight.Bold,
fontSize = 16.sp
)
Text(modifier = Modifier.fillMaxWidth(),
text = article.description ?: "",
color = Color.Black,
fontWeight = FontWeight.Medium,
fontSize = 14.sp
)
}
}
}
}
This composable function display an article in androidView
@Composable
fun ShowOnWebView(url:String) {
val context= LocalContext.current
AndroidView(factory = {
WebView(context).apply {
webViewClient= WebViewClient()
loadUrl(url)
}
})
}