Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)

Handling Input Method Visibility

When input focus moves into or out of an editable text field, Android shows or hides the input method (such as the on-screen keyboard) as appropriate. The system also makes decisions about how your UI and the text field appear above the input method. For example, when the vertical space on the screen is constrained, the text field might fill all space above the input method. For most apps, these default behaviors are all that's needed.

In some cases, though, you might want to more directly control the visibility of the input method and specify how you'd like your layout to appear when the input method is visible. This lesson explains how to control and respond to the input method visibility.

Show the Input Method When the Activity Starts

Although Android gives focus to the first text field in your layout when the activity starts, it does not show the input method. This behavior is appropriate because entering text might not be the primary task in the activity. However, if entering text is indeed the primary task (such as in a login screen), then you probably want the input method to appear by default.

To show the input method when your activity starts, add the android:windowSoftInputMode attribute to the <activity> element with the "stateVisible" value. For example:

       <application ... >
    <activity
        android:windowSoftInputMode="stateVisible" ... >
        ...
    </activity>
    ...
</application>
      

Note: If the user's device has an attached hardware keyboard, the soft input method does not appear.

Show the Input Method On Demand

If there is a method in your activity's lifecycle where you want to ensure that the input method is visible, you can use the InputMethodManager to show it.

For example, the following method takes a View in which the user should type something, calls requestFocus() to give it focus, then showSoftInput() to open the input method:

       public void showSoftKeyboard(View view) {
    if (view.requestFocus()) {
        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
    }
}
      

Note: Once the input method is visible, you should not programmatically hide it. The system hides the input method when the user finishes the task in the text field or the user can hide it with a system control (such as with the Back button).

Specify How Your UI Should Respond

When the input method appears on the screen, it reduces the amount of space available for your app's UI. The system makes a decision as to how it should adjust the visible portion of your UI, but it might not get it right. To ensure the best behavior for your app, you should specify how you'd like the system to display your UI in the remaining space.

To declare your preferred treatment in an activity, use the android:windowSoftInputMode attribute in your manifest's <activity> element with one of the "adjust" values.

For example, to ensure that the system resizes your layout to the available space—which ensures that all of your layout content is accessible (even though it probably requires scrolling)—use "adjustResize" :

       <application ... >
    <activity
        android:windowSoftInputMode="adjustResize" ... >
        ...
    </activity>
    ...
</application>
      

You can combine the adjustment specification with the initial input method visibility specification from above:

       <activity
        android:windowSoftInputMode="stateVisible|adjustResize" ... >
        ...
    </activity>
      

Specifying "adjustResize" is important if your UI includes controls that the user might need to access immediately after or while performing text input. For example, if you use a relative layout to place a button bar at the bottom of the screen, using "adjustResize" resizes the layout so the button bar appears above the input method.