Support keyboard navigation

In addition to soft input methods—such as on-screen keyboards—Android supports physical keyboards attached to the device. A keyboard offers a convenient mode for text input and 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 popular, and many users like to attach keyboard accessories to them.

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

Test your app

Users might already be able to 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 Button and 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, perform the following procedure:

  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.

    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 Keyboard and DPad are enabled.
  2. To test your app, use only the Tab key to navigate through your UI. Make sure each UI control gets focus as expected.

    Look for any instances in which the focus moves in an unexpected manner.

  3. Start again from the beginning of your app and navigate through your UI using direction controls like the arrow keys on the keyboard. From each focusable element in your UI, press Up, Down, Left, and Right.

    Look for any instances in which the focus moves in an unexpected manner.

If you encounter any instances where navigating with the Tab key or direction controls doesn't do what you expect, specify where the focus must be 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. To pass focus from the first button to the text field and then to the second button, the layout needs to explicitly define the focus order for each of the focusable elements with the android:nextFocusForward attribute.

<androidx.constraintlayout.widget.ConstraintLayout ...>
        ... />
        ... />
        ...  />

Now, instead of the focus moving from button1 to button2 and then editText1, it appropriately moves according to the appearance on the screen: from button1 to editText1 and then button2.

Handle directional navigation

Users can also navigate your app using the arrow keys on a keyboard, which behaves the same as when navigating with a D-pad or trackball. The system provides a "best guess" for which view to give focus to in a given direction based on the layout of the views on screen. However, sometimes the system guesses wrong.

If the system doesn't pass focus to the appropriate view when navigating in a given direction, specify which view must 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. This is shown in the following example:

    ... />
    ... />
    ...  />

Additional resources

Refer to the following related resources: