Data Binding is a powerful feature in Android that allows you to bind UI components in your layouts to data sources in your application using a declarative format. This helps minimize boilerplate code and keeps your UI updated automatically when the underlying data changes. By utilizing Data Binding with Jetpack Compose, you can create a more responsive and maintainable UI.
@Composable
fun UserProfile(user: User) {
val name = remember { mutableStateOf(user.name) }
val age = remember { mutableStateOf(user.age) }
Column {
TextField(
value = name.value,
onValueChange = { name.value = it },
label = { Text("Name") }
)
TextField(
value = age.value.toString(),
onValueChange = { age.value = it.toIntOrNull() ?: 0 },
label = { Text("Age") }
)
Button(onClick = { updateUserProfile(name.value, age.value) }) {
Text("Update Profile")
}
}
}
fun updateUserProfile(name: String, age: Int) {
// Logic to update the user profile
}