{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

SemanticsModifierKt

public final class SemanticsModifierKt


Summary

Public methods

static final @NonNull Modifier

Clears the semantics of all the descendant nodes and sets new semantics.

static final @NonNull Modifier
semantics(
    @NonNull Modifier receiver,
    boolean mergeDescendants,
    @ExtensionFunctionType @NonNull Function1<@NonNull SemanticsPropertyReceiverUnit> properties
)

Add semantics key/value pairs to the layout node, for use in testing, accessibility, etc.

Public methods

clearAndSetSemantics

public static final @NonNull Modifier clearAndSetSemantics(
    @NonNull Modifier receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull SemanticsPropertyReceiverUnit> properties
)

Clears the semantics of all the descendant nodes and sets new semantics.

In the merged semantics tree, this clears the semantic information provided by the node's descendants (but not those of the layout node itself, if any) and sets the provided semantics. (In the unmerged tree, the semantics node is marked with "SemanticsConfiguration.isClearingSemantics", but nothing is actually cleared.)

Compose's default semantics provide baseline usability for screen-readers, but this can be used to provide a more polished screen-reader experience: for example, clearing the semantics of a group of tiny buttons, and setting equivalent actions on the card containing them.

Parameters
@ExtensionFunctionType @NonNull Function1<@NonNull SemanticsPropertyReceiverUnit> properties

properties to add to the semantics. SemanticsPropertyReceiver will be provided in the scope to allow access for common properties and its values.

semantics

public static final @NonNull Modifier semantics(
    @NonNull Modifier receiver,
    boolean mergeDescendants,
    @ExtensionFunctionType @NonNull Function1<@NonNull SemanticsPropertyReceiverUnit> properties
)

Add semantics key/value pairs to the layout node, for use in testing, accessibility, etc.

The provided lambda receiver scope provides "key = value"-style setters for any SemanticsPropertyKey. Additionally, chaining multiple semantics modifiers is also a supported style.

The resulting semantics produce two SemanticsNode trees:

The "unmerged tree" rooted at SemanticsOwner.unmergedRootSemanticsNode has one SemanticsNode per layout node which has any SemanticsModifier on it. This SemanticsNode contains all the properties set in all the SemanticsModifiers on that node.

The "merged tree" rooted at SemanticsOwner.rootSemanticsNode has equal-or-fewer nodes: it simplifies the structure based on mergeDescendants and clearAndSetSemantics. For most purposes (especially accessibility, or the testing of accessibility), the merged semantics tree should be used.

Parameters
boolean mergeDescendants

Whether the semantic information provided by the owning component and its descendants should be treated as one logical entity. Most commonly set on screen-reader-focusable items such as buttons or form fields. In the merged semantics tree, all descendant nodes (except those themselves marked mergeDescendants) will disappear from the tree, and their properties will get merged into the parent's configuration (using a merging algorithm that varies based on the type of property -- for example, text properties will get concatenated, separated by commas). In the unmerged semantics tree, the node is simply marked with SemanticsConfiguration.isMergingSemanticsOfDescendants.

@ExtensionFunctionType @NonNull Function1<@NonNull SemanticsPropertyReceiverUnit> properties

properties to add to the semantics. SemanticsPropertyReceiver will be provided in the scope to allow access for common properties and its values.