Skip to content

Instantly share code, notes, and snippets.

@labibmuhajir
Created October 19, 2023 08:28
Show Gist options
  • Save labibmuhajir/f27fa3d7ce0380e0d012f3f5a38ae4d5 to your computer and use it in GitHub Desktop.
Save labibmuhajir/f27fa3d7ce0380e0d012f3f5a38ae4d5 to your computer and use it in GitHub Desktop.
compose in xml xml in compose
//If you want to use a Compose in your XML file, you can add this to your layout file:
<androidx.compose.ui.platform.ComposeView
android:id="@+id/my_composable"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
//and then, set the content:
findViewById<ComposeView>(R.id.my_composable).setContent {
MaterialTheme {
Surface {
Text(text = "Hello!")
}
}
}
//If you want the opposite, i.e. to use an XML file in your compose, you can use this:
AndroidView(
factory = { context ->
val view = LayoutInflater.from(context).inflate(R.layout.my_layout, null, false)
val textView = view.findViewById<TextView>(R.id.text)
// do whatever you want...
view // return the view
},
update = { view ->
// Update the view
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment