Hey everyone, I’m trying to figure out how to auto...
# compose
r
Hey everyone, I’m trying to figure out how to automatically focus a text field when it’s rendered. This is my current attempt but it’s not working. Is there documentation on TextField I could reference to figure this out?
Copy code
@ExperimentalFocus
@Composable
fun SignInEmailView() {
    val viewModel = viewModel<AuthenticationViewModel>()
    val text: String by viewModel.email.observeAsState("")
    val focusRequester = FocusRequester()

    SingleInputLayout(
        onBackClicked = {},
        onNextClicked = {}
    ) {
        TextField(
            value = text,
            onValueChange = viewModel::setEmail,
            label = { Text(text = "What's your email address?") },
            placeholder = { Text(text = "<mailto:jane.doe@gmail.com|jane.doe@gmail.com>") },
            keyboardType = KeyboardType.Email,
            modifier = frameFillWidth(60.dp).focusRequester(focusRequester),
            backgroundColor = MaterialTheme.colors.background
        )

        Spacer(modifier = frameFillWidth(200.dp))

        onActive(callback = {
            focusRequester.captureFocus()
        })
    }
}