What are those ImeActions?

IME Actions

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 ActionWhen to use?
NoneNo action is expected from the keyboard
DefaultLet the keyboard to decide the action
DoneUser is done providing input to a group of inputs
GoUser would like to go to the target of the text in the input
NextUser is done with the current input, and wants to move to the next one
PreviousUser wants to return to the previous input
SearchUser wants to execute a search
SendUser 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 ActionCompose propertyLambda name
NoneImeAction.None-
DefaultImeAction.Default-
DoneImeAction.DoneonDone
GoImeAction.GoonGo
NextImeAction.NextonNext
PreviousImeAction.PreviousonPrevious
SearchImeAction.SearchonSearch
SendImeAction.SendonSend

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 ActionXML property valueListener action name
NoneactionNone-
DefaultactionUnspecified-
DoneactionDoneEditorInfo.IME_ACTION_DONE
GoactionGoEditorInfo.IME_ACTION_GO
NextactionNextEditorInfo.IME_ACTION_NEXT
PreviousactionPreviousEditorInfo.IME_ACTION_PREVIOUS
SearchactionSearchEditorInfo.IME_ACTION_SEARCH
SendactionSendEditorInfo.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