6

I am using android compose jetpack version "0.1.0-dev14". According to application requirement, I want to auto focus TextField once screen is visible. and another scenario is like I want to focus next TextField in same screen on next ImeAction.Next action. I could not find any method or solution for this problem. If Anyone can help me to sole this problem, It will be really appreciated.

Thank you..!!

Madhav Gor
  • 211
  • 2
  • 12

2 Answers2

6

With 1.0.0 to achieve autofocus you can use:

DisposableEffect(Unit) {
    focusRequester.requestFocus()
    onDispose { }
}

In this way the system grants focus to the component associated with this FocusRequester. For example:

val focusRequester = FocusRequester()
TextField(
    //...
    modifier = Modifier
        // add focusRequester modifier 
        .focusRequester(focusRequester)
)
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
2

This sample shows how to request focus on the next view.

To achieve autofocus on first view on the screen, I believe this can be used:

// ... composable context...

onActive(callback = {
    focusModifiers.first().requestFocus()
})

// ... composable context...
Alex
  • 76
  • 2
  • 7