When input focus moves in 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 decides 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 more control over the visibility of the input method and how it impacts the layout. 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 doesn't 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>
Show the input method on demand
If there is a method in your activity's lifecycle where you want to ensure 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 is expected to
type something, calls
requestFocus()
to give it
focus, then calls
showSoftInput()
to open the input method:
Kotlin
fun showSoftKeyboard(view: View) { if (view.requestFocus()) { val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT) } }
Java
public void showSoftKeyboard(View view) { if (view.requestFocus()) { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); } }
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 decides how to adjust the visible portion of your UI, but it might not get it right. To ensure the best behavior for your app, specify how you want 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 keeps all of your layout content accessible, even if it
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 the preceding section:
<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.