Skip to content
DeveloperMemos

Using the Clickable Modifier in Jetpack Compose

Android, Jetpack Compose, User Interaction1 min read

Jetpack Compose is a declarative UI toolkit for Android app development. It provides a simple way to create interactive user interfaces using Kotlin code. In this article, we will explore the clickable modifier in Jetpack Compose and how it can be used to add click handling to your user interface elements.

What is the clickable modifier?

The clickable modifier is a built-in modifier in Jetpack Compose that enables click handling for a user interface element. It can be used with any UI element that can receive user input, such as a Button or a Text element. The clickable modifier is easy to use and provides a clean way to add click handling to your UI elements.

How to use the clickable modifier

Using the clickable modifier in Jetpack Compose is straightforward. You can simply apply the modifier to any UI element that you want to make clickable. Here is an example of how to use the clickable modifier with a Text element:

1Text(
2 text = "Click me!",
3 modifier = Modifier.clickable {
4 // Handle click event
5 }
6)

In this example, we apply the clickable modifier to a Text element and provide a lambda expression to handle the click event. Whenever the user clicks on the Text element, the lambda expression will be executed.

You can also provide a parameter to the clickable modifier to specify how the element should respond to click events. For example, you can specify that the element should only respond to single clicks or double clicks:

1Text(
2 text = "Click me!",
3 modifier = Modifier.clickable(
4 onClick = {
5 // Handle click event
6 },
7 ...
8 )
9)

Adding accessibility to clickable elements

It is important to make your app accessible to users with disabilities. When using the clickable modifier, you should also provide an accessibility label for the clickable element. This label should describe the action that will be performed when the element is clicked.

1Text(
2 text = "Click me!",
3 modifier = Modifier.clickable(
4 onClick = {
5 // Handle click event
6 },
7 onClickLabel = "Open the menu"
8 )
9)

In this example, we provide an accessibility label for the Text element that describes the action that will be performed when the user clicks on it.

Summary

  • The clickable modifier in Jetpack Compose enables click handling for UI elements.
  • The clickable modifier can be applied to any UI element that can receive user input.
  • When using the clickable modifier, it is important to also provide an accessibility label