2

I am trying to change the default focus from the top left to a different composable when the screen displays. I am attempting to get Talkback to read the text that is further down instead of the top-left button.

@Composable
fun Screen() {
    val requester = FocusRequester()
    Column {
        Top()
        Bottom(requester)
    }
    SideEffect {
        requester.requestFocus()
    }
}

@Composable
fun Top() {
    Row{
    Button(onClick = {}) {
        Text(text = "top left")
    }
    Button(onClick = {}) {
        Text(text = "top right")    
    }
}

@Composable
fun Bottom(requester: FocusRequester) {
    Column {
        Text(
             text = "Text to read",
             modifier = Modifier
                .focusRequester(requester)
                .focusable(true)
                .focusTarget()
        )
    }
}

This code will work if a LaunchedEffect is used and the requestFocus call is delayed for 550. I suspect the requestFocus is called during the compose phase, and so there is nothing drawn to the screen yet for the request to "grab onto", like if it was called after the draw phase.

Jeremy
  • 21
  • 3
  • Does this answer your question? [Request Focus on TextField in jetpack compose](https://stackoverflow.com/questions/64181930/request-focus-on-textfield-in-jetpack-compose) – Dan Harms May 17 '22 at 21:56
  • That doesn't seem to work. Modifier.focus() is unresolved reference and also Modifier.focusObserver. – Jeremy May 17 '22 at 22:23
  • I don't see either of those mentioned in the top answer in that link. – Dan Harms May 17 '22 at 22:30
  • My apologies, I was looking at the wrong reply. I tried the top answer and it still focuses on the top-left element. I also tried the next top answer from that question, and that also still focuses on the top-left element. – Jeremy May 17 '22 at 22:58

0 Answers0