https://kotlinlang.org logo
#compose
Title
# compose
l

Lauren Yew

09/07/2021, 9:42 PM
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

nitrog42

09/07/2021, 9:56 PM
Sorry if you did, but did you put the Modifier.focusable(true)/focusTarget modifier on the Text of the title ?
l

Lauren Yew

09/07/2021, 9:57 PM
I did, and I put those at the end as suggested.
Tried all combinations of that.
n

nitrog42

09/07/2021, 10:29 PM
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

Lauren Yew

09/07/2021, 10:35 PM
Thank you! I’ll give this a try.
n

nitrog42

09/07/2021, 10:41 PM
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

Lauren Yew

09/07/2021, 10:44 PM
Oh this is perfect, thank you! It makes sense that accessiblity talkback isn’t using regular focus.
2 Views