semantics Vs clearAndSetSemantics
When working on accessibility in a Jetpack Compose UI, you use semantics
and clearAndSetSemantics
to give the user information about the UI they are interacting with. But how do you know when to use semantics
vs clearAndSetSemantics
?
If an element has no children, you can use semantics
to override the default behavior for that element. If an element has children, but you just want it all read as a single logical element use semantics
but set the mergeDescendants
parameter to true
. If you use mergeDescendants
but want certain children to still read, you can also set mergeDescendants
to true
for those elements and they will not be merged into the parent. Finally, if you want to have a parent element to have a fully custom contentDescription
and don't want the children read, use clearAndSetSemantics
. Remember that even if you use mergeDescendants
and a custom contentDescription
in a semantics
modifier that the system will read both the custom description and the merged descendent description.
Want to see it in action? Check out this gist showing how TalkBack intreprets different modifiers.
That's all you need to know to start using semantics
and clearAndSetSemantics
in your app. I hope this helps you give your users the best possible experience. Until next time, thanks!
Did you find this content helpful?
Please share this post and be sure to subscribe to the RSS feed to be notified of all future articles!
Want to go above and beyond? Help me out by sending me $1 on Ko-fi. It goes a long way in helping run this site and keeping it advertisement free. Thank you in advance!
![Buy me a Coffee on Ko-fi](/img/Mi0wcVcqyX-200.webp)
- Previous: Introductions
- Next: Android Adaptive Icons