콘텐츠로 건너뛰기

자주 방문한 페이지

최근 방문한 페이지

navigation

Supporting Keyboard Navigation

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 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:

  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 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 button1 to button2 then editText1, the focus appropriately moves according to the appearance on the screen: from button1 to editText1 then 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"
    ...  />
이 사이트는 쿠키를 사용하여 사이트별 언어 및 표시 옵션에 대한 환경설정을 저장합니다.

Google Play에서 성공을 거두는 데 도움이 되는 최신 Android 개발자 뉴스 및 도움말을 받아 보세요.

* 필수 입력란

완료되었습니다.

WeChat에서 Google Developers 팔로우하기

이 사이트를 (으)로 탐색할까요?

페이지를 요청했지만 이 사이트의 언어 환경설정은 입니다.

언어 환경설정을 변경하고 이 사이트를 (으)로 탐색할까요? 언어 환경설정을 나중에 변경하려면 각 페이지 하단의 언어 메뉴를 사용하세요.

이 클래스를 사용하려면 API 수준 이상이 필요합니다.

문서에 대해 선택한 API 수준이 이므로 이 문서가 표시되지 않습니다. 왼쪽 탐색 메뉴의 선택기로 문서 API 수준을 변경할 수 있습니다.

앱에 필요한 API 수준 지정에 관한 자세한 내용은 다양한 플랫폼 버전 지원을 참조하세요.

Take a short survey?
Help us improve the Android developer experience. (April 2018 — Developer Survey)