Interoperability: Using VIEW‐BINDING in the compose - devrath/ComposeAlchemy GitHub Wiki

Dependency

implementation("androidx.compose.ui:ui-viewbinding:1.6.8")

Composable

@Composable
fun ViewsInComposeDemo(navController: NavHostController) {

    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(10.dp),
        horizontalAlignment = Alignment.CenterHorizontally,
        verticalArrangement = Arrangement.Center,
    ) {
        val context = LocalContext.current
        // Using the xml with view binding in compose
        AndroidViewBinding(SimpleViewOneBinding::inflate) {
            demoButtonId.setOnClickListener {
                Toast.makeText(context,"Button Clicked!",Toast.LENGTH_LONG).show()
            }
        }
    }

}

XML-VIEW

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/demoButtonId"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Demo Button"/>
</LinearLayout>