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.
Asked
Active
Viewed 1,353 times
6

Gabriele Mariotti
- 320,139
- 94
- 887
- 841

Kiran
- 63
- 4
-
1make it `singleLine = true` – EpicPandaForce Nov 02 '22 at 22:55
2 Answers
9
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 */})
)

Nikhil Dupally
- 753
- 1
- 4
- 12
3
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
}
)

Gabriele Mariotti
- 320,139
- 94
- 887
- 841