1 page.title=Handling Input Method Visibility 2 3 trainingnavtop=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> <li><a href="#ShowOnStart">Show the Input Method When the Activity Starts</a></li> <li><a href="#ShowOnDemand">Show the Input Method On Demand</a></li> <li><a href="#Respond">Specify How Your UI Should Respond</a></li> </ol> 16 17 </div> 18 </div> 19 20 21 <p>When input focus moves into or out of an editable text field, Android shows 22 or hides the input method (such as the on-screen keyboard) as appropriate. 23 The system also makes decisions about 24 how your UI and the text field appear above the input method. For example, when the vertical 25 space on the screen is constrained, the text field might fill all space above the input method. 26 For 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 29 the visibility of the input method and specify how you'd like your layout to appear 30 when the input method is visible. This lesson explains how to control and respond to 31 the 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 37 when the activity starts, it does not show the input method. This behavior is appropriate because 38 entering text might not be the primary task in the activity. However, if entering 39 text is indeed the primary task (such as in a login screen), then you probably want 40 the input method to appear by default.</p> 41 42 <p>To show the input method when your activity starts, add the <a 43 href="{@docRoot}guide/topics/manifest/activity-element.html#wsoft">{@code 44 android:windowSoftInputMode}</a> attribute to the {@code <activity>} element with the 45 {@code "stateVisible"} value. For example:</p> 46 47 <pre> 48 <application ... > 49 <activity 50 android:windowSoftInputMode="stateVisible" ... > 51 ... 52 </activity> 53 ... 54 </application> 55 </pre> 56 57 <p class="note"><strong>Note:</strong> If the user's device has an attached hardware keyboard, 58 the 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 64 the input method is visible, you can use the {@link android.view.inputmethod.InputMethodManager} 65 to show it.</p> 66 67 <p>For example, the following method takes a {@link android.view.View} in which the user should type 68 something, calls {@link android.view.View#requestFocus requestFocus()} to give it focus, then 69 {@link android.view.inputmethod.InputMethodManager#showSoftInput showSoftInput()} to open 70 the input method:</p> 71 72 <pre> 73 public 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> 83 Once the input method is visible, you should not programmatically hide it. The system 84 hides the input method when the user finishes the task in the text field or the user can hide 85 it 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 93 for your app's UI. The system makes a decision as to how it should adjust the visible portion 94 of your UI, but it might not get it right. To ensure the best behavior for your app, 95 you 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 98 href="{@docRoot}guide/topics/manifest/activity-element.html#wsoft">{@code 99 android:windowSoftInputMode}</a> attribute in your manifest's {@code <activity>} element 100 with one of the "adjust" values.</p> 101 102 <p>For example, to ensure that the system resizes your layout to the available space—which 103 ensures that all of your layout content is accessible (even though it probably requires 104 scrolling)—use {@code "adjustResize"}:</p> 105 106 <pre> 107 <application ... > 108 <activity 109 android:windowSoftInputMode="adjustResize" ... > 110 ... 111 </activity> 112 ... 113 </application> 114 </pre> 115 116 <p>You can combine the adjustment specification with the <a 117 href="#ShowOnStart">initial input method visibility</a> specification from above:</p> 118 119 <pre> 120 <activity 121 android:windowSoftInputMode="stateVisible|adjustResize" ... > 122 ... 123 </activity> 124 </pre> 125 126 127 <p>Specifying {@code "adjustResize"} is important if your UI includes controls that the 128 user might need to access immediately after or while performing text input. For example, 129 if 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