CoursesCompose Multiplatform, one screen from state to pixels

Puzzle 10 · 11 of 11

Can VoiceOver understand the bookmark?

A custom bookmark icon turns solid when you tap it. A sighted user sees the change; VoiceOver hears nothing. Give the icon a label, a role and a state, and follow them from Compose to what VoiceOver says on an iPhone.

All puzzles in this course

The problem

Puzzle 9 put the shared screen inside an iOS app and a native text field inside the screen. The native field already speaks to VoiceOver, Apple's screen reader, because UIKit built it. Everything else on the screen is Compose, drawn as pixels into one view (the KMP course, puzzle 15). Each saved item has a custom bookmark icon: an outline that turns solid when you tap it. A sighted user sees the change. A VoiceOver user moves a finger over the item and hears nothing. Why, and what has to change?

See it

Before: a bookmark icon with a raw tap handler turns solid on tap but has no semantics, and VoiceOver says nothing; after: the same icon with toggleable, role Switch and a content description has a label, a role and an on state, which iOS receives and VoiceOver reads as Bookmark Lake walk, switch button, on beforeLake walktap handler only semanticsnone( silence ) afterLake walktoggleable · Switch label: Bookmark Lake walkrole: switchstate: on “Bookmark Lake walk,switch button, on”

Scroll sideways to see the full diagram.

  1. Before: the icon reacts to a tap with a raw touch handler, and the picture turns from outline to solid. A sighted user sees the change.

    Step 1 of 5
  2. But nothing described it. VoiceOver reads only accessibility semantics: a label, a role, a state, actions. Here there are none, so VoiceOver has nothing to say.

    Step 2 of 5
  3. After: the same icon, now with Modifier.toggleable, Role.Switch and a content description. Predict: what can VoiceOver say now?

    Step 3 of 5
  4. Compose's semantics now hold a label, a role and an on/off state, and Compose Multiplatform maps them to the element's iOS accessibility label, traits and value.

    Step 4 of 5
  5. VoiceOver reads them out, roughly like this (the exact words depend on the iOS version and settings). A double-tap flips it, and it says "off".

    Step 5 of 5

The building block

VoiceOver can't look at pixels. It asks the app for accessibility semantics: a description of each element, made of a label (what it is), a role (what kind of control), a state (on or off) and the actions it supports. Compose keeps that description in its semantics tree (the KMP course, puzzle 15), and on iOS, JetBrains' docs say, "semantics data produced by Compose APIs is now mapped to native objects and properties that are consumed by iOS Accessibility Services".

Here is the icon as first written:

Icon(
    painter = bookmarkIcon(saved),           // outline or solid picture
    contentDescription = null,
    modifier = Modifier.pointerInput(Unit) {
        detectTapGestures { onToggle(!saved) }
    },
)

pointerInput with detectTapGestures reacts to raw touches and adds no semantics at all, and a contentDescription of null says the picture means nothing on its own. So the semantics tree has nothing here, and VoiceOver skips the icon, or can't say what it does. For VoiceOver the bookmark doesn't exist.

The fix is to describe the control, not the picture:

Icon(
    painter = bookmarkIcon(saved),
    contentDescription = null,               // decorative: the control below speaks for it
    modifier = Modifier
        .toggleable(
            value = saved,
            role = Role.Switch,
            onValueChange = onToggle,
        )
        .semantics { contentDescription = "Bookmark ${item.title}" },
)

Modifier.toggleable makes the element something that is either on or off, and reacts to a tap or to an accessibility action by asking to flip it (onValueChange). It adds the semantics with it: a toggleable state (on or off) and a click action. Role.Switch, passed as its role, says what kind of control it is. The semantics { } block adds the label: contentDescription, the text a screen reader speaks for an element.

What iOS receives, in Compose Multiplatform's iOS code: the label becomes the element's accessibility label; the toggleable state adds a toggle trait and the value "1" or "0"; and Role.Switch adds UIKit's toggle-button trait on iOS 17 and later. VoiceOver puts those together and says something like "Bookmark Lake walk, switch button, on". The exact words depend on the iOS version and the user's VoiceOver settings, so check them on a device.

Why a contentDescription of null on the icon? A screen reader treats a control as one element, and the label belongs to the control. If the picture also had a description, it could be read as well, or instead. Android's docs say a null description marks a graphic that is "purely decorative".

Material has this built in. IconToggleButton(checked = saved, onCheckedChange = onToggle) { Icon(bookmarkIcon(saved), contentDescription = "Bookmark ${item.title}") } is already toggleable, with the role of a checkbox, and it merges its children's semantics into itself, so the icon's description becomes the button's label. Use it unless your design needs a custom control.

A screenshot can't test any of this. Turn on VoiceOver on an iPhone, move to the bookmark, listen, double-tap, and listen again: the value should change from off to on. The native field from puzzle 9 is UIKit's own element, so check it the same way, but separately.

Go deeperWhere does Role.Switch become an iOS trait?
On an iPhone with iOS 16, the same bookmark is read without "switch button". What changed?

Compose Multiplatform's iOS code adds UIKit's toggle-button trait for Role.Switch only when the iOS version is 17 or later (the code checks the iOS version before adding it). The toggleable state still maps to a value, "1" or "0", and the label stays, so VoiceOver still says the name and on or off; only the role word is missing.

Back to the main puzzle

The analogy

The painted set from puzzle 9's theatre. A sighted audience sees a painted door that's open or closed. A blind audience member hears the play through an audio describer (VoiceOver), who can only say what the script tells them: "door to the garden, a switch, now open". If the script says nothing about the door, the describer says nothing, however well it is painted.

Check yourself

You keep the pointerInput version but give the Icon a content description, "Bookmark". VoiceOver focuses it. What can the user learn?

Not quite. An icon with a description gets a label and is read; it's the missing role and state that hurt here. Re-read that part ↑

Not quite. A label names an element; the role, the state and the action come from toggleable.

Right. A label alone doesn't make a control.

Why does the fixed version set the Icon's own contentDescription to null?

Not quite. It hides only the picture's own description; the toggleable element still has its label, role and state.

Not quite. Nothing throws; the problem is what VoiceOver says. Re-read that part ↑

Right. One control, one label.

A teammate replaces the custom icon with IconToggleButton and gives the Icon inside it the content description "Bookmark Lake walk". Is the label lost because it sits on the child?

Not quite. Material components come with semantics, and Compose Multiplatform maps them on iOS too. Re-read that part ↑

Not quite. Material's buttons merge their children's semantics into one element. Re-read that part ↑

Right. Merging lifts the child's label onto the control.

Sources
  • Kotlin docs: Support for iOS accessibility features ("Screen readers and VoiceOver can access the content of the Compose Multiplatform UI."; "This is possible because semantics data produced by Compose APIs is now mapped to native objects and properties that are consumed by iOS Accessibility Services. For most interfaces built with Material widgets, this should happen automatically.")
  • Android Developers: Semantics ("Information about the meaning and role of a component in Compose is called semantics"; "The Role indicates the type of element."; "The ToggleableState is the current state of the Switch."; "the clickable and toggleable modifiers will automatically merge their descendants.")
  • Android Developers: Merging and clearing (a custom toggle: ".toggleable(value = checked.value, onValueChange = { checked.value = it })" with "role = Role.Switch", and its Icon with "contentDescription = null // not needed here")
  • Android Developers: Accessibility API defaults ("Some graphic elements are purely decorative and you might not want to communicate them to the user. When you set the contentDescription parameter to null, you indicate to the Android framework that this element does not have associated actions or state.")
  • Compose Multiplatform source: SemanticConfigurationUtils.ios.kt ("if (contains(SemanticsProperties.ToggleableState)) { result = result or CMPAccessibilityTraitToggle }"; "Role.Switch -> { if (available(OS.Ios to OSVersion(major = 17))) { result = result or UIAccessibilityTraitToggleButton } }"; and for the value: "ToggleableState.On -> "1"", "ToggleableState.Off -> "0"")
  • Compose foundation source: Toggleable.kt (public fun Modifier.toggleable(value: Boolean, enabled: Boolean = true, role: Role? = null, interactionSource: MutableInteractionSource? = null, onValueChange: (Boolean) -> Unit); "Configure component to make it toggleable via input and accessibility events"); and material3's IconButton.kt, where IconToggleButton uses ".toggleable(… role = Role.Checkbox …)"

What you now know

Pixels don't speak. VoiceOver only knows what Compose's semantics say, and Compose Multiplatform maps those semantics to iOS accessibility properties. A tap handler with no semantics is silent; Modifier.toggleable(role = Role.Switch) plus a contentDescription gives VoiceOver a label, a switch role and an on/off value, and IconToggleButton does the same with Material's defaults. An icon inside a control that already has a label gets a null contentDescription.

Apply what you learned

Continue with worked examples and practice cases. When you reach the final project, make your own decisions before comparing your evidence with the worked review.

Continue to Trace identity, effects and accessible output → Start the final project →