orangy
Checkbox
but it doesn’t have a label. Of course, I can make a Row
with checkbox and text, but they won’t be semantically connected (and I need special handling for label tap to toggle checkbox). What is the proper way of making labeled checkbox with accessibility in mind?orangy
jim
07/20/2021, 1:44 PMmergeDescendants
to make descendants become semantically connected as described by @Bryan Herbst in his compose-semantics-talkback blog post:
A very common desire is to group multiple elements on screen together and have TalkBack read them as one element. For example if you have a Checkbox with a Text, you probably want to have both the state of the Checkbox and the text grouped together for accessibility purposes.enables that grouping! In themergeDescendants
world it is common to do this usingandroid.view
on aandroid:importantForAccessibility
.ViewGroup
Here’s a concrete example with a Checkbox:
Row(
Modifier.semantics(mergeDescendants = true) {}
) {
Checkbox(checked = true, onCheckedChange = {})
Text("Item one")
}
This will create a Row that is focusable for accessibility, and when it receives focus TalkBack will read “Checked, Item one”.
jim
07/20/2021, 1:46 PMBryan Herbst
07/20/2021, 1:49 PMmergeAllDescendants
is what you want!
The one caveat (and I need to update my post to reflect this) is that you want to set onCheckedChange
on your Checkbox()
to null. If you have a non-null checked change listener then TalkBack will still be able to focus the row and the checkbox separately.
More details here: https://issuetracker.google.com/issues/193414941Bryan Herbst
07/20/2021, 1:50 PMmergeAllDescendants
changed to just mergeDescendants
after I made that post, sorry I’m a bit behind on updating that)orangy
Alexandre Elias [G]
07/21/2021, 10:59 PMRadioGroupSample
illustrates how to do this accessibly (same pattern applies to Checkbox
and Switch
). Note that clickable
, selectable
and toggleable
modifiers all contain mergeDescendants
in their implementation so it is rarely need for non-library code to specify mergeDescendants
explicitly