page.title=Supporting Keyboard Navigation trainingnavtop=true @jd:body

This lesson teaches you to

  1. Test Your App
  2. Handle Tab Navigation
  3. Handle Directional Navigation

You should also read

In addition to soft input methods (such as on-screen keyboards), Android supports physical keyboards attached to the device. A keyboard offers not only a convenient mode for text input, but also offers a way for users to navigate and interact with your app. Although most hand-held devices such as phones use touch as the primary mode of interaction, tablets and similar devices are growing in popularity and many users like to attach keyboard accessories.

As more Android devices offer this kind of experience, it's important that you optimize your app to support interaction through a keyboard. This lesson describes how you can better support navigation with a keyboard.

Note: Supporting of directional navigation in your application is also important in ensuring that your application is accessible to users who do not navigate using visual cues. Fully supporting directional navigation in your application can also help you automate user interface testing with tools like uiautomator.

Test Your App

It's possible that users can already navigate your app using a keyboard, because the Android system enables most of the necessary behaviors by default.

All interactive widgets provided by the Android framework (such as {@link android.widget.Button} and {@link android.widget.EditText}) are focusable. This means users can navigate with control devices such as a D-pad or keyboard and each widget glows or otherwise changes its appearance when it gains input focus.

To test your app:

  1. Install your app on a device that offers a hardware keyboard.

    If you don't have a hardware device with a keyboard, connect a Bluetooth keyboard or a USB keyboard (though not all devices support USB accessories).

    You can also use the Android emulator:

    1. In the AVD Manager, either click New Device or select an existing profile and click Clone.
    2. In the window that appears, ensure that Keyboard and DPad are enabled.
  2. To test your app, use only the Tab key to navigate through your UI, ensuring that each UI control gets focus as expected.

    Look for any instances in which the focus moves in a way you don't expect.

  3. Start from the beginning of your app and instead use the direction controls (arrow keys on the keyboard) to navigate your app.

    From each focusable element in your UI, press Up, Down, Left, and Right.

    Look for any instances in which the focus moves in a way you don't expect.

If you encounter any instances where navigating with the Tab key or direction controls does not do what you expect, specify where the focus should go in your layout, as discussed in the following sections.

Handle Tab Navigation

When a user navigates your app using the keyboard Tab key, the system passes input focus between elements based on the order in which they appear in the layout. If you use a relative layout, for example, and the order of elements on the screen is different than the order in the file, then you might need to manually specify the focus order.

For example, in the following layout, two buttons are aligned to the right side and a text field is aligned to the left of the second button. In order to pass focus from the first button to the text field, then to the second button, the layout needs to explicitly define the focus order for each of the focusable elements with the {@code android:nextFocusForward} attribute:

<RelativeLayout ...>
    <Button
        android:id="@+id/button1"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:nextFocusForward="@+id/editText1"
        ... />
    <Button
        android:id="@+id/button2"
        android:layout_below="@id/button1"
        android:nextFocusForward="@+id/button1"
        ... />
    <EditText
        android:id="@id/editText1"
        android:layout_alignBottom="@+id/button2"
        android:layout_toLeftOf="@id/button2"
        android:nextFocusForward="@+id/button2"
        ...  />
    ...
</RelativeLayout>

Now instead of sending focus from {@code button1} to {@code button2} then {@code editText1}, the focus appropriately moves according to the appearance on the screen: from {@code button1} to {@code editText1} then {@code button2}.

Handle Directional Navigation

Users can also navigate your app using the arrow keys on a keyboard (the behavior is the same as when navigating with a D-pad or trackball). The system provides a best-guess as to which view should be given focus in a given direction based on the layout of the views on screen. Sometimes, however, the system might guess wrong.

If the system does not pass focus to the appropriate view when navigating in a given direction, specify which view should receive focus with the following attributes:

Each attribute designates the next view to receive focus when the user navigates in that direction, as specified by the view ID. For example:

<Button
    android:id="@+id/button1"
    android:nextFocusRight="@+id/button2"
    android:nextFocusDown="@+id/editText1"
    ... />
<Button
    android:id="@id/button2"
    android:nextFocusLeft="@id/button1"
    android:nextFocusDown="@id/editText1"
    ... />
<EditText
    android:id="@id/editText1"
    android:nextFocusUp="@id/button1"
    ...  />