What are those ImeActions?
It’s keyboard action button which is often skipped during the development and sometimes even in Q/A process. Most user may never use that, but you will make some other furious. In modern times you should care about all of them.
You should care even more when you are developing apps for
- Android TV - virtual keyboard is quite annoying, so it’s sad when we will have switch to virtual cursor to switch edit field
- Business - when employees are filling the same form for 100x time, it’s way easier to click next on keyboard than switch fields by touch
- Devices with physical or external keyboard - it’s just more pleasant
As a developer you have some possibilities to control those keyboard
ImeAction types and when to use
Ime Action | When to use? |
---|---|
None | No action is expected from the keyboard |
Default | Let the keyboard to decide the action |
Done | User is done providing input to a group of inputs |
Go | User would like to go to the target of the text in the input |
Next | User is done with the current input, and wants to move to the next one |
Previous | User wants to return to the previous input |
Search | User wants to execute a search |
Send | User wants to send the text in the input |
Implementation
Jetpack Compose
Instructions
There are several options when you are setting things up.
You have to set KeyboardOptions
on your TextField
.
TextField(
//...
keyboardOptions = KeyboardOptions(
imeAction = ImeAction.Next
)
)
You have to set KeyboardActions
on your TextField
TextField(
//...
keyboardActions = KeyboardActions(
onNext = {
// ...
}
)
)
Compose mappings
Ime Action | Compose property | Lambda name |
---|---|---|
None | ImeAction.None | - |
Default | ImeAction.Default | - |
Done | ImeAction.Done | onDone |
Go | ImeAction.Go | onGo |
Next | ImeAction.Next | onNext |
Previous | ImeAction.Previous | onPrevious |
Search | ImeAction.Search | onSearch |
Send | ImeAction.Send | onSend |
Full Snippet
@Preview
@Composable
fun PersonalInformationForm() {
val focusManager = LocalFocusManager.current
Column(
modifier = Modifier
.background(Color.White)
.padding(all = 6.dp)
) {
TextField(
value = "Enter first name",
onValueChange = { /** handle state **/ },
keyboardOptions = KeyboardOptions(
imeAction = ImeAction.Next
),
keyboardActions = KeyboardActions(
onNext = {
focusManager.moveFocus(FocusDirection.Down)
}
),
)
TextField(
value = "Enter surname",
onValueChange = { /** handle state **/ },
keyboardOptions = KeyboardOptions(
imeAction = ImeAction.Send
),
keyboardActions = KeyboardActions(
onSend = {
/** Send/Save data **/
}
)
)
Spacer(modifier = Modifier.height(6.dp))
Button(
modifier = Modifier.align(Alignment.CenterHorizontally),
onClick = { /** Send/Save data **/ }) {
Text("Send")
}
}
}
XML + Code
You have to set android:imeOptions
in your XML file under the EditText and listener to handle the action.
Snippets
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="emailAddress"
android:hint="@string/new_email"
android:imeOptions="actionSearch"
android:inputType="textEmailAddress"
android:maxLines="1"
android:textSize="16sp" />
binding.et_email.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
//... Search logic and else
}
}
XML + Code mappings
Ime Action | XML property value | Listener action name |
---|---|---|
None | actionNone | - |
Default | actionUnspecified | - |
Done | actionDone | EditorInfo.IME_ACTION_DONE |
Go | actionGo | EditorInfo.IME_ACTION_GO |
Next | actionNext | EditorInfo.IME_ACTION_NEXT |
Previous | actionPrevious | EditorInfo.IME_ACTION_PREVIOUS |
Search | actionSearch | EditorInfo.IME_ACTION_SEARCH |
Send | actionSend | EditorInfo.IME_ACTION_SEND |
Summary
You shouldn’t skip keyboard actions during the implementation. It may reduce accessibility or even make app unusable on some platforms.
Thank you for your time,
Konrad