Complete the code to add a traditional Android Button inside a Compose UI using AndroidView.
AndroidView(factory = { context ->
val button = Button(context)
button.text = "Click me"
button.setOnClickListener { /* handle click */ }
[1]
})The AndroidView factory must return the Android View to be displayed. Here, returning button shows the Button inside Compose.
Complete the code to update the Android TextView's text inside AndroidView's update block.
AndroidView(factory = { context ->
TextView(context).apply { text = "Hello" }
}, update = { textView ->
textView.[1] = "Updated Text"
})The text property of TextView sets the displayed text. Using textView.text = "Updated Text" updates the text inside Compose.
Fix the error in the code to correctly add a SeekBar inside Compose using AndroidView.
AndroidView(factory = { context ->
val seekBar = SeekBar(context)
seekBar.max = 100
seekBar.progress = 50
[1]
})In Kotlin lambdas with labels, return@factory seekBar explicitly returns the SeekBar from the factory lambda, which is required here.
Fill both blanks to create an Android EditText with a hint and update its text inside Compose.
AndroidView(factory = { context ->
EditText(context).apply {
[1] = "Enter name"
}
}, update = { editText ->
editText.[2] = "John Doe"
})setText() instead of text for the update block.contentDescription instead of hint.The hint property sets the placeholder text in EditText. To update the text, use the text property inside the update block.
Fill all three blanks to create an Android CheckBox with a label, set its checked state, and update it inside Compose.
AndroidView(factory = { context ->
CheckBox(context).apply {
text = [1]
isChecked = [2]
}
}, update = { checkBox ->
checkBox.isChecked = [3]
})The CheckBox label is set with the text property. The initial checked state is set with isChecked. Inside update, you can change isChecked to update the UI.