I want the text field on my chat screen to be focused by default (the cursor should be visible) when the user opens the screen. However, I do not want the keyboard to open automatically. The keyboard should appear only when the user explicitly taps on the text field.
I’m trying to achieve behavior similar to WhatsApp.
I’ve searched online and gone through the documentation, but I couldn’t find a helpful solution.
val msg = remember { mutableStateOf(TextFieldValue()) }
TextField(
modifier = Modifier.fillMaxWidth(),
value = msg .value,
onValueChange = { value : TextFieldValue ->
msg .value = value
}
)
This is NOT possible in Jetpack Compose exactly like WhatsApp..
Reason:
If a TextField is focused → keyboard automatically opens
If you prevent keyboard → focus is also not shown
So both together (cursor visible + no keyboard) is not supported by Android system behavior.
What you can do (closest solution)
You can request focus but hide keyboard manually:
val focusRequester = remember { FocusRequester() }
val keyboardController = LocalSoftwareKeyboardController.current
LaunchedEffect(Unit) {
focusRequester.requestFocus()
keyboardController?.hide() // hide keyboard
}
TextField(
value = msg.value,
onValueChange = { msg.value = it },
modifier = Modifier
.fillMaxWidth()
.focusRequester(focusRequester)
)