truth seeker
truth seeker

Reputation: 45

Capturing/passing webview clicks on surface composable

I want my webview to not respond to any touches; I just want it to display content(hence the touchListener returning true). The parent Surface composable should handle the touches. But it doesn't work.

@OptIn(ExperimentalMaterialApi::class)
@Preview
@Composable
fun MyComposable() {
    Surface(
        modifier = Modifier.size(250.dp).padding(10.dp),
        onClick = { Log.e("TEST", "Clicked") },
        elevation = 5.dp
    )  {
       AndroidView(
           factory = { context ->
               WebView(context).apply {
                   this.setOnTouchListener { _, _ -> true }
               }
           },
           update = {
               it.loadUrl("https://stackoverflow.com/")
           }
       )
    }
}

Upvotes: 1

Views: 1002

Answers (1)

truth seeker
truth seeker

Reputation: 45

Currently the only solution that works is having an empty Surface composable on top of the WebView to capture the clicks.

@OptIn(ExperimentalMaterialApi::class)
@Preview
@Composable
fun MyComposable() {
    Surface(
        modifier = Modifier.size(250.dp).padding(10.dp),
//        onClick = { Log.e("TEST", "Clicked") },    // Removed
        elevation = 5.dp
    )  {
       AndroidView(
           factory = { context ->
               WebView(context).apply {
                   this.setOnTouchListener { _, _ -> true }
               }
           },
           update = {
               it.loadUrl("https://stackoverflow.com/")
           }
       )

       Surface(
           onClick = { Log.e("TEST", "clicked") },
           color = Color.Transparent,
           modifier = Modifier.fillMaxSize()
       ) { }
    }
}

But this solution seems a bit hacky. Better solutions are welcome.

Upvotes: 2

Related Questions