Search code examples
androidkotlinandroid-jetpack-composeandroid-compose-textfield

How to restrict Keyboard Enter key not to allow input in the next line (TextField in Android Jetpack Compose)


In Android emulator, entering input using Computer Keyboard, but the "Enter" key on my keyboard should take the input and do the action. Instead, the input is allowing me in the next line, and keep on continuing to the next line (as a new line character). Please suggest me your answers in Android Jetpack Composable of TextField element.


Solution

  • Hey you can use singleLine = true in text field, To perform any custom actions on click of Done/Enter Key in keyboard you can use something like

        TextField(
        value = text,
        onValueChange = {text = it},
        keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
        keyboardActions = KeyboardActions(
            onDone = { /* do something */},
        onGo = { /* do something */},
        onNext = { /* do something */},
        onPrevious = { /* do something */},
        onSearch = { /* do something */},
        onSend = { /* do something */})
    )