Composables
Docs Login
Tally

Segmented Control

Jetpack Compose Component

Download
package com.composables.tally.components

import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.selection.selectableGroup
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.unit.dp
import com.composables.compose.ripple.rememberRippleIndication
import com.composables.tally.theme.Space
import com.composables.tally.theme.Theme

@Composable
fun SegmentedControl(
    options: List<String>,
    selected: Int,
    onSelect: (Int) -> Unit,
    modifier: Modifier = Modifier,
) {
    Row(
        modifier
            .fillMaxWidth()
            .clip(Theme.shapes.fullyRounded)
            .background(Theme.colors.surfaceVariant)
            .padding(Space)
            .selectableGroup()
    ) {
        options.forEachIndexed { index, option ->
            val isSelected = index == selected
            val container by
                animateColorAsState(
                    if (isSelected) Theme.colors.surface else Theme.colors.surfaceVariant,
                    tween(150),
                    label = "Segment",
                )
            Box(
                Modifier.weight(1f)
                    .heightIn(min = 44.dp)
                    .clip(Theme.shapes.fullyRounded)
                    .background(container)
                    .selectable(
                        selected = isSelected,
                        onClick = { onSelect(index) },
                        role = Role.Tab,
                        interactionSource = remember { MutableInteractionSource() },
                        indication = rememberRippleIndication(color = Theme.colors.onSurface),
                    )
                    .padding(horizontal = Space * 2),
                contentAlignment = Alignment.Center,
            ) {
                Text(
                    option,
                    style = Theme.typography.label,
                    color =
                        if (isSelected) Theme.colors.onSurface else Theme.colors.onBackgroundMuted,
                    maxLines = 1,
                )
            }
        }
    }
}

More components from Tally

by Alex Styl

Search screens and components

Screen flows

Screens

UI Elements

One Android lesson, every Wednesday

Building Android apps?

What Alex learned building Android apps for 14 years, so you don't have to. Reads in under a minute.

Join 3,500+ Android developers. Unsubscribe any time.

You're subscribed

The next lesson lands in on Wednesday.