androidandroid-jetpack-composesemantics

TextField onValueChange - Announce Additional Semantics


Using a custom TextField implementation, but it still uses the BasicTextField provided by compose.

BasicTextField(
    modifier = Modifier
        .fillMaxWidth()
        .semantics(mergeDescendants = true) {
            this.contentDescription = (contentDescription
                ?: "") + (if (!placeholder.isNullOrBlank() && value.isBlank()) placeholder
                else value) + counterText
            if (disabled) {
               disabled()
            }
        },
    value = value,
    onValueChange = { newValue ->
        onValueChange(newValue)
    },
    enabled = enabled,
    readOnly = readOnly,
    textStyle = mergedTextStyle,
    cursorBrush = SolidColor(contentColor),
    keyboardOptions = keyboardOptions,
    keyboardActions = keyboardActions,
    interactionSource = interactionSource,
    singleLine = singleLine,
    maxLines = maxL,
    minLines = minL
) {...

The semantics read exactly as I would like them to on original focus. But when I type a character all it reads is that character.

key entered, A = “A”.

I would like the semantics to announce “A, ${counterText}”. But I can’t find a way to tie into what looks to be the TextInputService or something similar to that. Is there a concept I’m missing?


Solution

  • So I found what I was looking for. It’s annoying because I can’t seem to find examples on the Compose Developer instructions, but it works pretty well.

    Modifier.semantics(mergeDescendants = true) {
        …
        liveRegion = LiveRegionMode.Polite // ..Assertive if you want immediate response on change.
    }
    

    With this the Text will say ”A, $stateDescription, $contentDescription”