Has anyone found a way to observe Talkback focus i...
# compose
a
Has anyone found a way to observe Talkback focus in jetpack compose ? There seems to be some workaround with using an
AndroidView
and
AccessibilityEvent
but that code is not too nice, Any ideas?
c
This would be so helpful to find. This is a big issue in Android TV... 😅
👍 2
a
what is talkback focus? do mean u want to know which element is currently focused?
a
Exactly. It would be great if there was a similar thing to:
Copy code
val focused = interactionSource.collectIsFocusedAsState()
c
The thing with focus and talkback focus is there is a distinct difference, even though it looks the same. The regular focus follows everything the FocusManager does, and can be manipulated or detected by using modifiers like
onFocusChanged
,
focusProperties
,
focusRequest
,
focusRestorer
,... Talkback focus does not follow this, and just does whatever the hell it wants... 😅 As far as I can tell, it just let's the user go to any location that contains text or has proper semantics. Not giving any callback as to what is focused, so there is no way to know what is getting focused and when. And also no way to manipulate the views or data if needed.
😅 1
a
I use this for debugging focus
Copy code
@Composable
private fun Modifier.debugFocusRing(): Modifier {
    if (isDevelopment.not()) return this

    var wasFocused by remember { mutableStateOf(false) }

    return this.onFocusChanged { wasFocused = wasFocused.not() }
        .border(thickness = 1.dp, Color.Red)
}
you would need to apply this to all items that can be focused
d
This would be for debugging "normal" focus like people have already mentioned above, not Screen Reader focus. I can only echo what Christiano has said - there is no good way to control Screen Reader focus in Compose. At this point I'm not sure if that's intentional or not - perhaps a Screen Reader user expects a consistent experience across different apps and one way to achieve this is to not allow the experience to be adjusted by devs.