Handling User Input in Jetpack Compose: A Complete Guide
Jetpack Compose is a modern toolkit for building Android user interfaces. One essential aspect of any user interface is handling user input. In this blog post, we’ll explore how to handle user input in Jetpack Compose through various examples, starting from basic to advanced use cases.
Basic User Input: Text Fields
The most common user input scenario involves text fields. Jetpack Compose provides the TextField and OutlinedTextField components to create text fields. Here's a basic example:
@Preview
@Composable
fun UserInputExample() {
val text = remember { mutableStateOf("") }
Column {
TextField(
value = text.value,
onValueChange = { newText -> text.value = newText },
label = { Text("Enter your name") }
)
Text("Hello, ${text.value}!")
}
}In this example, we use a TextField to accept the user's name as input. We use a mutableStateOf to store the input and update it with the onValueChange function.
Checkboxes
Checkboxes are another common user input component. Jetpack Compose provides the Checkbox component to create them. Here's an example:
@Preview
@Composable
fun CheckboxExample() {
val checked = remember { mutableStateOf(false) }
Row {
Checkbox(
checked = checked.value,
onCheckedChange = { newChecked -> checked.value = newChecked }
)
Text("I accept the terms and conditions")
}
}In this example, we use a Checkbox and a Text inside a Row to create a simple terms and conditions acceptance input.
Radio Buttons
Radio buttons allow the user to select one option from a list. Jetpack Compose provides the RadioButton component to create radio buttons. Here's an example:
@Preview
@Composable
fun RadioButtonExample() {
val options = listOf("Option 1", "Option 2", "Option 3")
val selectedOption = remember { mutableStateOf(options[0]) }
Column {
options.forEach { option ->
Row {
RadioButton(
selected = option == selectedOption.value,
onClick = { selectedOption.value = option }
)
Text(option)
}
}
}
}In this example, we create a list of options and use the RadioButton component to create a radio button for each option. We use a mutableStateOf to store the selected option and update it with the onClick function.
Slider
Sliders are useful for allowing users to select a value within a range. Jetpack Compose provides the Slider component for this purpose. Here's an example:
@Preview
@Composable
fun SliderExample() {
val sliderValue = remember { mutableStateOf(0f) }
Column {
Text("Select a value:")
Slider(
value = sliderValue.value,
onValueChange = { newValue -> sliderValue.value = newValue },
valueRange = 0f..100f,
steps = 100
)
Text("Selected value: ${sliderValue.value.toInt()}")
}
}In this example, we use a Slider to allow users to select a value between 0 and 100. We use a mutableStateOf to store the slider value and update it with the onValueChangeonValueChange` function.
Dropdown Menu
Dropdown menus allow users to select a single option from a list. Jetpack Compose provides the DropdownMenu and DropdownMenuItem components to create dropdown menus. Here's an example:
@Preview
@Composable
fun DropdownMenuExample() {
val options = listOf("Option 1", "Option 2", "Option 3")
val selectedOption = remember { mutableStateOf(options[0]) }
val menuExpanded = remember { mutableStateOf(false) }
Column {
TextButton(onClick = { menuExpanded.value = true }) {
Text("Select an option")
}
DropdownMenu(
expanded = menuExpanded.value,
onDismissRequest = { menuExpanded.value = false }
) {
options.forEach { option ->
DropdownMenuItem(onClick = {
selectedOption.value = option
menuExpanded.value = false
}) {
Text(option)
}
}
}
Text("Selected option: ${selectedOption.value}")
}
}In this example, we create a list of options and use DropdownMenu and DropdownMenuItem components to create a dropdown menu. We use mutableStateOf to store the selected option and update it with the onClick function.
Handling Gestures
Jetpack Compose provides gesture detection components to handle user interactions, such as taps, long presses, swipes, and more. Let’s create a simple example of handling tap gestures:
@Preview
@Composable
fun GestureDetectorExample() {
val tapCount = remember { mutableStateOf(0) }
Column {
Text(
"Tap me!",
modifier = Modifier.clickable {
tapCount.value++
}
)
Text("Taps: ${tapCount.value}")
}
}In this example, we use the clickable modifier to detect tap gestures on a Text component. We use a mutableStateOf to store the tap count and update it with the tap gesture callback.
If you enjoyed the article and would like to show your support, be sure to:
👏 Applaud for the story (50 claps) to help this article get featured
👉Follow me on Medium
Check out more content on my Medium profile





