1page.title=Handling Input Method Visibility
2
3trainingnavtop=true
4
5@jd:body
6
7<div id="tb-wrapper">
8<div id="tb">
9
10<h2>This lesson teaches you to</h2>
11<ol>
12  <li><a href="#ShowOnStart">Show the Input Method When the Activity Starts</a></li>
13  <li><a href="#ShowOnDemand">Show the Input Method On Demand</a></li>
14  <li><a href="#Respond">Specify How Your UI Should Respond</a></li>
15</ol>
16
17</div>
18</div>
19
20
21<p>When input focus moves into or out of an editable text field, Android shows
22or hides the input method (such as the on-screen keyboard) as appropriate.
23The system also makes decisions about
24how your UI and the text field appear above the input method. For example, when the vertical
25space on the screen is constrained, the text field might fill all space above the input method.
26For most apps, these default behaviors are all that's needed.</p>
27
28<p>In some cases, though, you might want to more directly control
29the visibility of the input method and specify how you'd like your layout to appear
30when the input method is visible. This lesson explains how to control and respond to
31the input method visibility.</p>
32
33
34<h2 id="ShowOnStart">Show the Input Method When the Activity Starts</h2>
35
36<p>Although Android gives focus to the first text field in your layout
37when the activity starts, it does not show the input method. This behavior is appropriate because
38entering text might not be the primary task in the activity. However, if entering
39text is indeed the primary task (such as in a login screen), then you probably want
40the input method to appear by default.</p>
41
42<p>To show the input method when your activity starts, add the <a
43href="{@docRoot}guide/topics/manifest/activity-element.html#wsoft">{@code
44android:windowSoftInputMode}</a> attribute to the {@code &lt;activity>} element with the
45{@code "stateVisible"} value. For example:</p>
46
47<pre>
48&lt;application ... >
49    &lt;activity
50        android:windowSoftInputMode="stateVisible" ... >
51        ...
52    &lt;/activity>
53    ...
54&lt;/application>
55</pre>
56
57<p class="note"><strong>Note:</strong> If the user's device has an attached hardware keyboard,
58the soft input method <em>does not</em> appear.</p>
59
60
61<h2 id="ShowOnDemand">Show the Input Method On Demand</h2>
62
63<p>If there is a method in your activity's lifecycle where you want to ensure that
64the input method is visible, you can use the {@link android.view.inputmethod.InputMethodManager}
65to show it.</p>
66
67<p>For example, the following method takes a {@link android.view.View} in which the user should type
68something, calls {@link android.view.View#requestFocus requestFocus()} to give it focus, then
69{@link android.view.inputmethod.InputMethodManager#showSoftInput showSoftInput()} to open
70the input method:</p>
71
72<pre>
73public void showSoftKeyboard(View view) {
74    if (view.requestFocus()) {
75        InputMethodManager imm = (InputMethodManager)
76                getSystemService(Context.INPUT_METHOD_SERVICE);
77        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
78    }
79}
80</pre>
81
82<p class="note"><strong>Note:</strong>
83Once the input method is visible, you should not programmatically hide it. The system
84hides the input method when the user finishes the task in the text field or the user can hide
85it with a system control (such as with the <em>Back</em> button).</p>
86
87
88
89
90<h2 id="Respond">Specify How Your UI Should Respond</h2>
91
92<p>When the input method appears on the screen, it reduces the amount of space available
93for your app's UI. The system makes a decision as to how it should adjust the visible portion
94of your UI, but it might not get it right. To ensure the best behavior for your app,
95you should specify how you'd like the system to display your UI in the remaining space.</p>
96
97<p>To declare your preferred treatment in an activity, use the <a
98href="{@docRoot}guide/topics/manifest/activity-element.html#wsoft">{@code
99android:windowSoftInputMode}</a> attribute in your manifest's {@code &lt;activity>} element
100with one of the "adjust" values.</p>
101
102<p>For example, to ensure that the system resizes your layout to the available space&mdash;which
103ensures that all of your layout content is accessible (even though it probably requires
104scrolling)&mdash;use {@code "adjustResize"}:</p>
105
106<pre>
107&lt;application ... >
108    &lt;activity
109        android:windowSoftInputMode="adjustResize" ... >
110        ...
111    &lt;/activity>
112    ...
113&lt;/application>
114</pre>
115
116<p>You can combine the adjustment specification with the <a
117href="#ShowOnStart">initial input method visibility</a> specification from above:</p>
118
119<pre>
120    &lt;activity
121        android:windowSoftInputMode="stateVisible|adjustResize" ... >
122        ...
123    &lt;/activity>
124</pre>
125
126
127<p>Specifying {@code "adjustResize"} is important if your UI includes controls that the
128user might need to access immediately after or while performing text input. For example,
129if you use a relative layout to place a button bar at the bottom of the screen, using
130{@code "adjustResize"} resizes the layout so the button bar appears above the input method.</p>
131
132
133
134
135
136
137
138