A Button composable is a clickable UI element in Jetpack Compose that triggers an action when pressed. It is used to create buttons in Android apps with declarative UI.
Click to reveal answer
beginner
How do you create a simple Button with text "Click me" in Jetpack Compose?
Use the Button composable with a lambda for onClick and a Text composable inside it:
Button(onClick = { /* action */ }) {
Text("Click me")
}
Click to reveal answer
beginner
What parameter must you always provide to a Button composable?
You must always provide the onClick parameter, which is a lambda function that defines what happens when the button is pressed.
Click to reveal answer
intermediate
How can you customize the colors of a Button in Jetpack Compose?
You can customize colors by using the colors parameter with ButtonDefaults.buttonColors(), for example:
Button(
onClick = { },
colors = ButtonDefaults.buttonColors(containerColor = Color.Red)
) { Text("Red Button") }
Click to reveal answer
intermediate
What is the difference between Button and TextButton in Jetpack Compose?
Button has a background and elevation by default, making it look like a raised button. TextButton is a button without background or elevation, appearing as just text that is clickable.
Click to reveal answer
Which parameter is required when creating a Button composable?
Ashape
Btext
Ccolor
DonClick
✗ Incorrect
The onClick parameter defines the action when the button is pressed and is required.
How do you add text inside a Button composable?
AUse the Text composable inside the Button's lambda
BSet the text parameter of Button
CUse a string directly as Button's argument
DUse a TextView inside Button
✗ Incorrect
Button composable takes a lambda where you can place child composables like Text.
What does ButtonDefaults.buttonColors() help you do?
ASet button size
BAdd icons to button
CCustomize button colors
DChange button font
✗ Incorrect
ButtonDefaults.buttonColors() provides color customization options for buttons.
Which composable looks like a flat text without background by default?
ATextButton
BButton
COutlinedButton
DIconButton
✗ Incorrect
TextButton is a button styled as flat text without background or elevation.
What happens if you omit the onClick parameter in a Button composable?
AButton shows default action
BCompilation error
CButton works but does nothing
DButton is disabled automatically
✗ Incorrect
onClick is a required parameter; omitting it causes a compilation error.
Explain how to create a basic Button composable with a click action and text label.
Think about how you tell the button what to do and what it shows.
You got /3 concepts.
Describe ways to customize the appearance of a Button in Jetpack Compose.
Colors and styles can be changed using built-in helpers.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a Button composable in Android Jetpack Compose?
easy
A. To show lists of items
B. To display images on the screen
C. To create text input fields
D. To let users tap and trigger actions
Solution
Step 1: Understand the role of Button composable
The Button composable is designed to be tapped by users to perform an action.
Step 2: Compare with other UI elements
Other options like displaying images or text input are done by different composables, not Button.
Final Answer:
To let users tap and trigger actions -> Option D
Quick Check:
Button = triggers actions on tap [OK]
Hint: Buttons are for user taps that do something [OK]
Common Mistakes:
Confusing Button with Image or TextField
Thinking Button displays content only
Assuming Button is for layout only
2. Which of the following is the correct syntax to create a Button with a label "Click Me" in Jetpack Compose?
easy
A. Button(onClick = { Text("Click Me") })
B. Button { onClick = { } Text("Click Me") }
C. Button(onClick = { }) { Text("Click Me") }
D. Button(Text("Click Me"), onClick = { })
Solution
Step 1: Recall Button syntax in Compose
The Button composable takes an onClick lambda and a content lambda for the label.
Step 2: Check each option
Button(onClick = { }) { Text("Click Me") } correctly places onClick and Text inside Button's trailing lambda. Others misuse syntax or parameter order.
A. onClick expects a lambda, but println("Clicked") is called immediately
B. Text composable cannot be inside Button
C. Button requires a label parameter, missing here
D. onClick cannot print text
Solution
Step 1: Analyze onClick parameter
onClick expects a lambda () -> Unit, but println("Clicked") is a function call executed immediately.
Step 2: Understand correct lambda usage
It should be onClick = { println("Clicked") } to pass a lambda that runs println when clicked.
Final Answer:
onClick expects a lambda, but println("Clicked") is called immediately -> Option A
Quick Check:
onClick needs lambda, not immediate call [OK]
Hint: Use curly braces {} for onClick lambda, not direct call [OK]
Common Mistakes:
Calling function instead of passing lambda
Misunderstanding onClick parameter type
Thinking Text inside Button causes error
5. You want a Button that disables itself after being clicked once. Which approach correctly implements this behavior?
hard
A. Use a state variable to track clicks and set enabled = false after first click
B. Call Button(enabled = false) directly without state
C. Put enabled = false inside onClick lambda
D. Use Text composable with onClick instead of Button
Solution
Step 1: Understand Button enabled property
The Button composable has an enabled parameter to control if it can be clicked.
Step 2: Use state to track clicks
To disable after first click, use a state variable that changes on click and updates enabled.
Step 3: Evaluate options
Use a state variable to track clicks and set enabled = false after first click correctly uses state to disable button after click. Others ignore state or misuse enabled.
Final Answer:
Use a state variable to track clicks and set enabled = false after first click -> Option A
Quick Check:
State controls enabled to disable button after click [OK]
Hint: Use state to toggle enabled property after click [OK]
Common Mistakes:
Setting enabled false without state causes permanent disable
Trying to disable inside onClick without state
Using Text instead of Button for clickable UI
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.