Jetpack compose Toggle Button

Cristian Gomez
Nov 17, 2021

--

This will be a quick post, just a small component if you need a toggle button.

The challenge is to create a button like this:

Toggle Button design

We can list a couple of features here:

  • Multiple buttons can be aligned into the same component.
  • Buttons can be single or multiple select, or just clickable.
  • Rounded corners over the overall group.
  • Dividers between buttons.

The code for this is:

For achieving the first design we can use it in this way:

class MyButton {
private val options = arrayOf(
ToggleButtonOption(
"Projects",
iconRes = R.drawable.ic_star,
),
ToggleButtonOption(
"Upcoming",
iconRes = R.drawable.ic_upcoming,
),
)

@Composable
fun Component(onClick: (selection: Selection) -> Unit) {
ToggleButton(
options = options,
type = SelectionType.SINGLE,
modifier = Modifier.padding(end = 4.dp),
onClick = onClick
)
}
}

This is all, Thanks!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Cristian Gomez
Cristian Gomez

No responses yet

Write a response