Skip to main content

CamoSegmentedControl

Concept​

1. Why​

A segmented control switches between 2–5 closely related options in a compact row: "Day / Week / Month", "List / Grid", or a filter set. Material calls it segmented buttons, and Apple segmented control. It supports single selection (like a compact radio group) and multi-selection (like a row of toggle buttons).

2. Shape​

Material mapping preview: M3 segmented buttons.

  • Height 40, with an outline border and shapes.full ends.
  • The selected segment is secondaryContainer / onSecondaryContainer, with a check icon (for single selection only when there's no icon).

3. API​

Contract​

fun <T> CamoSegmentedControl(
options: List<Segment<T>>,
selected: T, // single selection
onSelectionChange: (T) -> Unit,
enabled: Boolean = true,
)
data class Segment<T>(val value: T, val label: String? = null, val icon: Widget? = null,
val contentDescription: String? = null, val enabled: Boolean = true)

A segment needs a label or an icon. Icon-only segments need a contentDescription.

Building blocks (ADR-30): CamoSegmentedContainer(content: Slot) (the track, outer shape and radio-group semantics) and CamoSegmentButton(segment, selected, onClick, position) (one skin-styled segment with its radio semantics). They let a design put custom content in segments (an icon with a count, two-line segments) without losing the look or keyboard behavior.

State → renderer (M5 build): the renderer has two parts, matching the building blocks: the container (SegmentedContainerState(enabled, count), the track around the segments in a row) and each segment (SegmentState(selected, enabled, position: ItemPosition, interaction) with SegmentSlots(label?, icon?, content?)). Core gives the segments equal widths.

Tokens used​

  • colors.outline, secondaryContainer, onSecondaryContainer, onSurface
  • shapes.full, typography.labelLarge
  • motion.durationShort (selection change; the Apple dialect slides its thumb)

Component theme​

theme.components.segmentedControl: SegmentedControlTheme?. Every field is nullable in the theme. The skin's defaults(theme) fills whatever isn't set (Material defaults shown). See Theme §3.10.

FieldTypeMaterial defaultWhat it's for
heightDp40control height
shapeShapeshapes.fullouter corners
borderWidthDp1outline and dividers
textStyleCamoTextStylelabelLargesegment labels
iconSizeDp18segment icons
showCheckIconBooleantrueMaterial's check mark on selected segments
colorsSegmentedColors(container, border, selectedContainer, selectedContent, content)transparent, outline, secondaryContainer, onSecondaryContainer, onSurfaceevery color it uses; container is the track (Minimal's surfaceContainer, added in the M5 build)

Accessibility​

  • Single selection: a radio group, with each segment a radio ("Week, 2 of 3, selected"). Arrow keys move and select.
  • Each segment has a 48 minimum touch target, and the control grows vertically if needed.

4. Build steps​

  1. Segment, CamoSegmentedControl, and the selection logic.
  2. SegmentedControlRenderer, and the DebugSkin renderer.
  3. Tests:
    • a tap selects one;
    • arrow keys move and select;
    • the announcements;
    • an icon-only segment without a description fails a debug assertion.

Done when

  • "Day / Week / Month" works by touch, keyboard and screen reader.

5. Edge cases​

CaseDecided behavior
More than 5 segments, or long labelsGuidance: use CamoTabs or CamoSelect. Labels ellipsize after one line, and every segment takes an equal width.
Multi-selectionNot a segmented control (ADR-23): Apple's iOS segmented control and Carbon's content switcher are single-select only. Use a row of filter chips, CamoChip(selected = …), which every dialect draws.
Font scale 200%The height grows. The labels still get one line with an ellipsis, and the full label is in semantics.
RTLThe segment order mirrors.
Custom segmentsCamoSegmentedContainer + CamoSegmentButton (ADR-30). The container keeps arrow-key navigation across its buttons.

Implementation​

1. Why​

A single-choice row of equal segments (ADR-23 removed multi-select). In Compose it's a Row of selectable segments with equal widths, the roving focus from CamoRadio, and public building blocks for custom segments (ADR-30).

2. Shape​

3. API​

@Composable
public fun <T> CamoSegmentedControl(
options: List<Segment<T>>,
selected: T,
onSelectionChange: (T) -> Unit,
modifier: Modifier = Modifier,
enabled: Boolean = true,
)

// building blocks (ADR-30)
@Composable public fun CamoSegmentedContainer(modifier: Modifier = Modifier, content: @Composable RowScope.() -> Unit)
@Composable public fun <T> RowScope.CamoSegmentButton(
segment: Segment<T>, selected: Boolean, onClick: () -> Unit, position: ItemPosition,
modifier: Modifier = Modifier, content: (@Composable () -> Unit)? = null, // null → the segment's label/icon
)
  • Equal widths: each CamoSegmentButton applies Modifier.weight(1f); the container uses Modifier.height(IntrinsicSize.Min) so dividers and the selected fill span the full height.
  • Selection animation: the renderer draws the selected background as one moving element (animateDpAsState over the measured segment offsets), so skins like Cupertino get the sliding thumb; Minimal fades instead.
  • Semantics: container selectableGroup(); each button selectable(selected, role = Role.RadioButton) with ItemPosition(index, count) exposed through collectionItemInfo.
  • Keyboard: the container installs the roving focus helper (one Tab stop, arrows move and select).

4. Build steps​

  1. Segment, SegmentedControlTheme / Style, the building blocks, then CamoSegmentedControl built from them.
  2. The DebugSkin renderer (bordered boxes, the selected one filled).
  3. commonTest: equal widths with uneven labels; tap selects; arrows select; icon-only segment without contentDescription asserts; a custom-content segment keeps radio semantics.

Done when

  • "Day / Week / Month" and a custom segment row (icon + count) work by touch, keyboard and screen reader.

5. Edge cases​

CaseDecided behavior
Labels too long for equal widthsOne line with an ellipsis; the full label stays in semantics.
Font scale 200%The row grows in height (intrinsic measurement), labels still single-line.
selected not in optionsNothing selected, debug log.