Has anyone tried setting focus on the `TopAppBar`?...
# compose
l
Has anyone tried setting focus on the
TopAppBar
? I'm trying to make it auto focus on the
title
rather than the
navigationIcon
, but none of the focus methods are working. I've tried setting
focusOrder
,
focusRequester
, using
DisposableEffect
to requestFocus. Nothing is working. Talkback always wants to focus first on the navigation icon of the back button. I have a design request to focus on the title THEN the back button.
n
Sorry if you did, but did you put the Modifier.focusable(true)/focusTarget modifier on the Text of the title ?
l
I did, and I put those at the end as suggested.
Tried all combinations of that.
n
I actually don't think focus modifier will help you, it's either not possible or the answer lies in the Modifier.semantics
It's probably the worst example to follow but...
Copy code
var hideBack by remember { mutableStateOf(true) }
LaunchedEffect(true) {
    delay(1_000L)
    hideBack = false
}

TopAppBar(title = {
    Text(
        "title", modifier = Modifier
            .focusable()
    )
}, navigationIcon = {
    IconButton(
        onClick = { }, modifier = Modifier.run {
            if (hideBack) {
                clearAndSetSemantics { }
            } else {
                this
            }
        }) {
        Icon(Icons.Default.ArrowBack, null)
    }
})
I think it does what you want ? I really hope somebody as a better answer 😄
l
Thank you! I’ll give this a try.
n
OK I think I understand better how it works, actually it's not really your screen that can define the order of what to focus first, my guess is that talkback always choose the first top left (or right depending of rlt) focusable element (compose or not compose)
and using views it seems you need to use method sendAccessibilityEvent and that it's not easy too https://stackoverflow.com/questions/28472985/android-set-talkback-accessibility-focus-to-a-specific-view
l
Oh this is perfect, thank you! It makes sense that accessiblity talkback isn’t using regular focus.