13

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.

1
  • 1
    make it singleLine = true Commented Nov 2, 2022 at 22:55

2 Answers 2

20

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 */})
)
Sign up to request clarification or add additional context in comments.

Comments

6

You can add singleLine = true to avoid the new line.

Also you can use the onKeyEvent modifier to intercept the ENTER and do a custom action. You can apply the same action also to the Done Key in keyboard using the KeyboardActions attribute.

Something like:

TextField(
    value = text,
    onValueChange = {text = it},
    singleLine = true ,
    keyboardOptions = KeyboardOptions(imeAction = ImeAction.Done),
    keyboardActions = KeyboardActions(
        onDone = { /* do something */}
    ),
    modifier = Modifier.onKeyEvent {
        if (it.key == Key.Enter){
            /* do something */
            true
        }
        false
    }
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.