Android App Polishing: Customizing Soft Keyboard Action Button

With the release of Capitaine Train for Android[0], several people recently asked me how we implemented some of the tips & tricks available in the application (some of these tricks are barely visible but remember, I love details :p). In order to showcase some of the most interesting application bits of code, I thought it could be helpful to Android developers to start a small series of posts. Feel free to comment this article if you want me to describe something you found nice in the Capitaine Train application. 

A long long time ago, Android 1.6 introduced soft input methods. Soft input methods has many advantages over traditional hardware keyboards. It obviously requires less hardware and may help to build lighter devices. It also allow content-dependent keyboards. For instance, a keyboard may be different when typing a person name, an email address or a pin code.

By default, the Android framework bundles several constants you may use to control the keyboard that will appear. From the code perspective, this can be controlled with the android:inputType[1] XML attributes on TextView. You can also change the "Enter" button to display a text that better match its purpose. This can be done thanks to the android:imeOptions attribute (see [2] for an exhaustive list of all the possible values):

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"
    android:inputType="textPassword" />

While a "Done" button can fit almost all purposes (log in form, sign up form, etc.) it is quite ambiguous and not really satisfying to my mind. At Capitaine Train we wanted the action button to better reflect the actual action it performs. As a consequence, when signing in, we wanted the button to display "Sign in" rather that "Done". Because that's what it does after all!

Android lets you customize the action button thanks to the android:imeActionLabel[3] and android:imeActionId[4] attributes (or their TextView#setActionLabelId(CharSequence, int)[5] Java equivalent). Using these methods basically consists on setting a label to the done button and a unique id used to identify the action. Here is what your XML layout should be look like:

<EditText
    android:id="@+id/password"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:imeActionId="@+id/action_sign_in"
    android:imeActionLabel="@string/sign_in_short"
    android:inputType="textPassword" />

The second part consists on catch all taps on the soft input keyboard action button made with the appropriate action id:

mEditText = (EditText) view.findViewById(R.id.password);
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
   @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == R.id.action_sign_in) {
            // Do sign in
            return true;
        }
        return false;
    }
});

#gde   #android   #article    #AndroidAppPolishing

[0]: https://play.google.com/store/apps/details?id=com.capitainetrain.android
[1]: http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
[2]: http://developer.android.com/reference/android/widget/TextView.html#attr_android:imeOptions
[3]: http://developer.android.com/reference/android/widget/TextView.html#attr_android:imeActionLabel
[4]: http://developer.android.com/reference/android/widget/TextView.html#attr_android:imeActionId
[5]: http://developer.android.com/reference/android/widget/TextView.html#setImeActionLabel(java.lang.CharSequence,%20int)
Photo
Shared publiclyView activity