Problem and Solutions
OutlinedTextField Autofocus in android ?
By M.K. Verma · 4 May 2022

To place the cursor at the end of the text, you must use TextFieldValue
The following code, give the focus to the TextField and place the cursor at the end of the text :
@Composable
fun TestTextField(title: String = "Hello") {
val titleFocusRequester = remember { FocusRequester() }
val titleTextFieldValueState = remember {
mutableStateOf(
TextFieldValue(
text = title,
selection = TextRange(title.length)
)
)
}
LaunchedEffect(Unit) {
titleFocusRequester.requestFocus()
}
Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
TextField(
modifier = Modifier
.fillMaxWidth()
.focusRequester(titleFocusRequester),
value = titleTextFieldValueState.value,
onValueChange = { tfv: TextFieldValue ->
titleTextFieldValueState.value = tfv
}
)
}
}