Yokan / rebuild
Course 1 8624561

17-accessibility.md

.codex/skills/compose-kotlin-agent-skills/references/17-accessibility.md

documentation / design asset200 lines
1---2name: accessibility3description: >4  Compose accessibility — contentDescription, semantics, mergeDescendants, touch targets,5  TalkBack traversal, WCAG contrast, live regions, Espresso a11y checks. Use when auditing UI.6version: "2.2.0"7updated: "2026-05-21"8---910# Accessibility — Compose, TalkBack, WCAG1112## Minimum standards1314| Rule | Value |15|---|---|16| Touch target | **48dp × 48dp** minimum (Material) |17| Text contrast (WCAG AA) | **4.5:1** normal text, **3:1** large text (18sp+ bold / 24sp+) |18| Decorative images | `contentDescription = null` |19| Functional icons | Meaningful `contentDescription` or `clearAndSetSemantics` |2021---2223## WRONG / RIGHT 1 — Missing contentDescription on functional IconButton2425```kotlin26// WRONG — TalkBack says "Button" with no context27IconButton(onClick = onDelete) {28    Icon(Icons.Default.Delete, contentDescription = null)29}3031// RIGHT32IconButton(onClick = onDelete) {33    Icon(34        imageVector = Icons.Default.Delete,35        contentDescription = stringResource(R.string.delete_item)36    )37}38```3940---4142## WRONG / RIGHT 2 — Redundant semantics on clickable Row4344```kotlin45// WRONG — double announcement: row + child text46Row(47    modifier = Modifier48        .clickable(onClick = onClick)49        .padding(16.dp)50) {51    Text("Account settings")52    Icon(Icons.Default.ChevronRight, contentDescription = null)53}5455// RIGHT — merge descendants into single node56Row(57    modifier = Modifier58        .clickable(onClick = onClick)59        .semantics(mergeDescendants = true) {}60        .padding(16.dp)61) {62    Text(stringResource(R.string.account_settings))63    Icon(Icons.Default.ChevronRight, contentDescription = null)64}65```6667---6869## WRONG / RIGHT 3 — Touch target under 48dp7071```kotlin72// WRONG — 24dp hit area fails WCAG / Material73IconButton(74    onClick = onClose,75    modifier = Modifier.size(24.dp)76) {77    Icon(Icons.Default.Close, contentDescription = stringResource(R.string.close))78}7980// RIGHT — minimumInteractiveComponentSize or explicit 48dp81IconButton(82    onClick = onClose,83    modifier = Modifier84        .minimumInteractiveComponentSize()85        .size(48.dp)86) {87    Icon(Icons.Default.Close, contentDescription = stringResource(R.string.close))88}89```9091---9293## WRONG / RIGHT 4 — Custom component missing semantics9495```kotlin96// WRONG — TalkBack skips custom toggle97@Composable98fun CustomToggle(checked: Boolean, onToggle: (Boolean) -> Unit) {99    Box(100        modifier = Modifier101            .size(48.dp, 28.dp)102            .background(if (checked) Color.Green else Color.Gray)103            .clickable { onToggle(!checked) }104    )105}106107// RIGHT — clearAndSetSemantics with role + state108@Composable109fun CustomToggle(110    checked: Boolean,111    onToggle: (Boolean) -> Unit,112    modifier: Modifier = Modifier113) {114    Box(115        modifier = modifier116            .size(48.dp, 28.dp)117            .toggleable(value = checked, onValueChange = onToggle)118            .semantics {119                role = Role.Switch120                stateDescription = if (checked) {121                    "On"122                } else {123                    "Off"124                }125            }126            .background(if (checked) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.surfaceVariant)127    )128}129```130131---132133## WRONG / RIGHT 5 — Dynamic updates not announced (live region)134135```kotlin136// WRONG — error text appears silently for TalkBack137if (error != null) {138    Text(text = error, color = MaterialTheme.colorScheme.error)139}140141// RIGHT — live region for assertive announcements142if (error != null) {143    Text(144        text = error,145        color = MaterialTheme.colorScheme.error,146        modifier = Modifier.semantics {147            liveRegion = LiveRegionMode.Assertive148        }149    )150}151```152153---154155## WRONG / RIGHT 6 — Espresso test without accessibility checks156157```kotlin158// WRONG — only checks visibility159composeTestRule.onNodeWithText("Save").assertIsDisplayed()160161// RIGHT — semantics + touch target in UI test162composeTestRule.onNodeWithContentDescription("Save changes")163    .assertIsDisplayed()164    .assertHasClickAction()165    .performClick()166167// Instrumentation — AccessibilityChecks (Espresso)168AccessibilityChecks.enable().apply {169    setRunChecksFromRootView(true)170}171onView(withId(R.id.main)).check(matches(isDisplayed()))172```173174---175176## Contrast check (WCAG AA)177178```kotlin179// Verify in theme — onSurface on surface must be ≥ 4.5:1180// Use Android Studio Layout Inspector → Accessibility Scanner181// Or compute: ColorUtils.calculateContrast(foreground, background) >= 4.5182183@Composable184fun AccessibleBody(text: String) {185    Text(186        text = text,187        style = MaterialTheme.typography.bodyLarge,188        color = MaterialTheme.colorScheme.onSurface  // designed for 4.5:1 on surface189    )190}191```192193## Audit checklist (link to compose sub-skill REVIEW MODE)1941951. Every interactive control has label or `mergeDescendants`1962. Decorative icons: `contentDescription = null`1973. Touch targets ≥ 48dp1984. Error/status uses `LiveRegionMode` where needed1995. Traversal order matches visual order (avoid `focusProperties` hacks unless needed)2006. UI tests use `onNodeWithContentDescription` not raw text when label differs201

Search chapters, reference and source filenames.