How to programmatically set focus on TextField in Jetpack Compose?
Hi everyone! I've just started learning Jetpack Compose and encountered a problem that in classic Android View was solved simply by calling `requestFocus()`.
I need that when opening a screen or clicking a button, the focus automatically moves to a specific input field (TextField). For example, there is a search screen where the user should immediately start typing without clicking on the input field.
Here is my current code:
```kotlin
@Composable
fun SearchScreen() {
var query by remember { mutableStateOf("") }
TextField(
value = query,
onValueChange = { query = it },
label = { Text("Search") }
)
Button(onClick = { /* need to set focus back to TextField */ }) {
Text("Reset and focus")
}
}
```
No answers yet.
Log in to leave an answer
Blogs