Bird
Raised Fist0
Android Kotlinmobile~20 mins

Button composable in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Button Composable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What happens when this Button composable is clicked?
Consider this Kotlin Compose code snippet for a Button. What will be the visible result after clicking the button once?
Android Kotlin
var count by remember { mutableStateOf(0) }
Button(onClick = { count++ }) {
  Text(text = "Clicked: $count")
}
AThe button label updates to show "Clicked: 1" after the first click.
BThe button label stays "Clicked: 0" even after clicking.
CThe app crashes due to missing state initialization.
DThe button disappears after clicking.
Attempts:
2 left
💡 Hint
Remember that mutableStateOf triggers UI recomposition when changed.
📝 Syntax
intermediate
2:00remaining
Which Button composable code snippet is syntactically correct?
Select the option that compiles without errors in Jetpack Compose.
AButton { onClick = { println("Clicked") } Text("Press") }
BButton(onClick = println("Clicked")) { Text("Press") }
CButton(onClick = { println("Clicked") } Text("Press"))
DButton(onClick = { println("Clicked") }) { Text("Press") }
Attempts:
2 left
💡 Hint
The onClick parameter requires a lambda expression.
❓ lifecycle
advanced
2:00remaining
What is the effect of calling setContent inside an Activity multiple times with Button composables?
If an Android Activity calls setContent twice, each time setting a Button composable, what happens to the UI?
AThe app crashes due to multiple setContent calls.
BBoth Buttons appear stacked vertically on the screen.
CThe second setContent replaces the first UI completely, showing only the second Button.
DThe first Button remains visible and the second is ignored.
Attempts:
2 left
💡 Hint
setContent replaces the entire Compose UI content of the Activity.
❓ navigation
advanced
2:00remaining
How to navigate to a new screen on Button click using Jetpack Compose Navigation?
Given a Button inside a composable, which onClick implementation correctly navigates to a "details" screen using NavController?
Android Kotlin
val navController = rememberNavController()
Button(onClick = { /* navigation code here */ }) {
  Text("Go to Details")
}
AonClick = { navController.navigate("details") }
BonClick = { navController.navigateTo("details") }
ConClick = { navController.navigateUp() }
DonClick = { navController.popBackStack("details", false) }
Attempts:
2 left
💡 Hint
Use the navigate() method with the route string to move to a new screen.
🔧 Debug
expert
2:00remaining
Why does this Button not update its label when clicked?
Analyze the code below. Why does the Button label not change after clicking?
Android Kotlin
var count = 0
Button(onClick = { count++ }) {
  Text(text = "Clicked: $count")
}
ABecause the Button's onClick lambda is empty and does not increment count.
BBecause 'count' is not a state variable, Compose does not recompose the UI on change.
CBecause the Text composable does not support string interpolation.
DBecause the Button composable requires a modifier to update its content.
Attempts:
2 left
💡 Hint
Compose only updates UI when state variables change.

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

  1. Step 1: Understand the role of Button composable

    The Button composable is designed to be tapped by users to perform an action.
  2. Step 2: Compare with other UI elements

    Other options like displaying images or text input are done by different composables, not Button.
  3. Final Answer:

    To let users tap and trigger actions -> Option D
  4. 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

  1. Step 1: Recall Button syntax in Compose

    The Button composable takes an onClick lambda and a content lambda for the label.
  2. 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.
  3. Final Answer:

    Button(onClick = { }) { Text("Click Me") } -> Option C
  4. Quick Check:

    Button(onClick) { Text } syntax [OK]
Hint: Button(onClick) { Text("label") } is correct pattern [OK]
Common Mistakes:
  • Putting onClick inside Button's content lambda
  • Passing Text as a parameter instead of inside lambda
  • Wrong order or missing braces
3. What will be displayed when this code runs?
Button(onClick = { println("Tapped") }) {
  Text("Press Here")
}
medium
A. A text label "Press Here" with no button functionality
B. A button labeled "Press Here" that prints "Tapped" when clicked
C. A button labeled "Tapped" that prints "Press Here" when clicked
D. An error because println cannot be used inside onClick

Solution

  1. Step 1: Identify the Button label

    The Button shows the Text composable with label "Press Here".
  2. Step 2: Understand onClick behavior

    When tapped, the onClick lambda runs println("Tapped"), printing to console.
  3. Final Answer:

    A button labeled "Press Here" that prints "Tapped" when clicked -> Option B
  4. Quick Check:

    Button label = "Press Here", onClick prints "Tapped" [OK]
Hint: Button label is Text; onClick runs code inside braces [OK]
Common Mistakes:
  • Confusing label text with printed output
  • Thinking println causes UI error
  • Assuming onClick changes label text
4. This code tries to create a Button but causes a compile error:
Button(onClick = println("Clicked")) {
  Text("Tap")
}
What is the error?
medium
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

  1. Step 1: Analyze onClick parameter

    onClick expects a lambda () -> Unit, but println("Clicked") is a function call executed immediately.
  2. Step 2: Understand correct lambda usage

    It should be onClick = { println("Clicked") } to pass a lambda that runs println when clicked.
  3. Final Answer:

    onClick expects a lambda, but println("Clicked") is called immediately -> Option A
  4. 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

  1. Step 1: Understand Button enabled property

    The Button composable has an enabled parameter to control if it can be clicked.
  2. Step 2: Use state to track clicks

    To disable after first click, use a state variable that changes on click and updates enabled.
  3. 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.
  4. Final Answer:

    Use a state variable to track clicks and set enabled = false after first click -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard