1 page.title=Onscreen Input Methods 2 @jd:body 3 4 5 <div id="qv-wrapper"> 6 <div id="qv"> 7 8 <h2>See also</h2> 9 <ol> 10 <li><a href="{@docRoot}resources/articles/creating-input-method.html">Creating an Input 11 Method</a></li> 12 <li><a href="{@docRoot}resources/samples/SoftKeyboard/index.html">Soft Keyboard sample</a></li> 13 </ol> 14 15 </div> 16 </div> 17 18 19 <p>Starting from Android 1.5, the Android platform offers an Input Method 20 Framework (IMF) that lets you create on-screen input methods such as software 21 keyboards. This article provide an overview of what Android input method editors 22 (IMEs) are and what an application needs to do to work well with them. The IMF 23 is designed to support new classes of Android devices, such as those without 24 hardware keyboards, so it is important that your application works well with the 25 IMF and offers a great experience for users.</p> 26 27 <h3>What is an input method?</h3> 28 29 <p>The Android IMF is designed to support a variety of IMEs, including soft 30 keyboard, hand-writing recognizers, and hard keyboard translators. Our focus, 31 however, will be on soft keyboards, since this is the kind of input method that 32 is currently part of the platform.</p> 33 34 <p>A user will usually access the current IME by tapping on a text view to 35 edit, as shown here in the home screen:</p> 36 37 <img style="width: 320px; height: 480px; margin-right: 10px;" src="images/on-screen-inputs_004.png"> 38 <img style="width: 320px; height: 480px;" src="images/on-screen-inputs.png"> 39 40 <p>The soft keyboard is positioned at the bottom of the screen over the 41 application's window. To organize the available space between the application 42 and IME, we use a few approaches; the one shown here is called <em>pan and 43 scan</em>, and simply involves scrolling the application window around so that 44 the currently focused view is visible. This is the default mode, since it is the 45 safest for existing applications.</p> 46 47 <p>Most often the preferred screen layout is a <em>resize</em>, where the 48 application's window is resized to be entirely visible. An example is shown 49 here, when composing an e-mail message:</p> 50 51 <img style="width: 320px; height: 480px; margin-right: 10px;" src="images/on-screen-inputs_005.png"> 52 <img style="width: 320px; height: 480px;" src="images/on-screen-inputs_003.png"> 53 54 <p>The size of the application window is changed so that none of it is hidden by 55 the IME, allowing full access to both the application and IME. This of course 56 only works for applications that have a resizeable area that can be reduced to 57 make enough space, but the vertical space in this mode is actually no less than 58 what is available in landscape orientation, so very often an application can 59 already accommodate it.</p> 60 61 <p>The final major mode is <em>fullscreen</em> or <em>extract</em> 62 mode. This is used when the IME is too large to reasonably share space 63 with the underlying application. With the standard IMEs, you will only 64 encounter this situation when the screen is in a landscape orientation, 65 although other IMEs are free to use it whenever they desire. In this 66 case the application window is left as-is, and the IME simply displays 67 fullscreen on top of it, as shown here:</p> 68 69 <img style="width: 480px; height: 320px; margin-right: 10px; margin-bottom: 10px;" src="images/on-screen-inputs_006.png"> 70 <img style="width: 480px; height: 320px;" src="images/on-screen-inputs_002.png"> 71 72 <p>Because the IME is covering the application, it has its own editing area, 73 which shows the text actually contained in the application. There are also some 74 limited opportunities the application has to customize parts of the IME (the 75 "done" button at the top and enter key label at the bottom) to improve the user 76 experience.</p> 77 78 <h3>Basic XML attributes for controlling IMEs</h3> 79 80 <p>There are a number of things the system does to try to help existing 81 applications work with IMEs as well as possible, such as:</p> 82 83 <ul> 84 <li>Use pan and scan mode by default, unless it can reasonably guess that 85 resize mode will work by the existence of lists, scroll views, etc.</li> 86 <li>Analyze the various existing TextView attributes to guess at the kind of 87 content (numbers, plain text, etc) to help the soft keyboard display an 88 appropriate key layout.</li> 89 <li>Assign a few default actions to the fullscreen IME, such as "next field" 90 and "done".</li> 91 </ul> 92 93 <p>There are also some simple things you can do in your application that will 94 often greatly improve its user experience. Except where explicitly mentioned, 95 these will work in any Android platform version, even those previous to Android 96 1.5 (since they will simply ignore these new options).</p> 97 98 <h4>Specifying each EditText control's input type</h4> 99 100 <p>The most important thing for an application to do is to use the new 101 <code>android:inputType</code> 102 attribute on each <code>EditText</code>. The attribute provides much richer 103 information 104 about the text content. This attribute actually replaces many existing 105 attributes (<code>android:</code><code>password</code>, 106 <code>android:</code><code>singleLine</code>, 107 <code>android:</code><code>numeric</code>, 108 <code>android:</code><code>phoneNumber</code>, 109 <code>android:</code><code>capitalize</code>, 110 <code>android:</code><code>autoText</code>, and 111 <code>android:</code><code>editable</code>). If you specify the older attributes 112 and the new <code>android:inputType</code> attribute, the system uses 113 <code>android:inputType</code> and ignores the others. </p> 114 115 <p>The <code>android:inputType</code> attribute has three pieces:</p> 116 117 <ul> 118 <li>The <em>class</em> is the overall interpretation of characters. The 119 currently supported classes are <code>text</code> (plain text), 120 <code>number</code> (decimal number), <code>phone</code> (phone number), and 121 <code>datetime</code> (a date or time).</li> 122 <li>The <em>variation</em> is a further refinement on the class. In the 123 attribute you will normally specify the class and variant together, with the 124 class as a prefix. For example, <code>textEmailAddress</code> is a text field 125 where the user will enter something that is an e-mail address (foo (a] bar.com) so 126 the key layout will have an '@' character in easy access, and 127 <code>numberSigned</code> is a numeric field with a sign. If only the class is 128 specified, then you get the default/generic variant.</li> 129 <li>Additional <em>flags</em> can be specified that supply further refinement. 130 These flags are specific to a class. For example, some flags for the 131 <code>text</code> class are <code>textCapSentences</code>, 132 <code>textAutoCorrect</code>, and <code>textMultiline</code>.</li> 133 </ul> 134 135 <p>As an example, here is the new EditText for the IM application's message text view:</p> 136 137 <pre> <EditText android:id="@+id/edtInput" 138 android:layout_width="0dip" 139 android:layout_height="wrap_content" 140 android:layout_weight="1" 141 android:inputType="textShortMessage|textAutoCorrect|textCapSentences|textMultiLine" 142 android:imeOptions="actionSend|flagNoEnterAction" 143 android:maxLines="4" 144 android:maxLength="2000" 145 android:hint="@string/compose_hint"/></pre> 146 147 <p>A full description of all of the input types can be found in the 148 documentation. It is important to make use of the correct input types that are 149 available, so that the soft keyboard can use the optimal keyboard layout for the 150 text the user will be entering.</p> 151 152 <h4>Enabling resize mode and other window features</h4> 153 154 <p>The second most important thing for your app to do is to specify the overall 155 behavior of your window in relation to the input method. The most visible aspect 156 of this is controlling resize vs. pan and scan mode, but there are other things 157 you can do as well to improve your user experience.</p> 158 159 <p>You will usually control this behavior through the 160 <code>android:windowSoftInputMode</code> attribute on each 161 <code><activity></code> definition in your 162 <code>AndroidManifest.xml</code>. Like the input type, there are a couple 163 different pieces of data that can be specified here by combining them 164 together:</p> 165 166 <ul> 167 <li>The window adjustment mode is specified with either 168 <code>adjustResize</code> or <code>adjustPan</code>. It is highly recommended 169 that you always specify one or the other.</li> 170 <li>You can further control whether the IME will be shown automatically when 171 your activity is displayed and other situations where the user moves to it. The 172 system won't automatically show an IME by default, but in some cases it can be 173 convenient for the user if an application enables this behavior. You can request 174 this with <code>stateVisible</code>. There are also a number of other state 175 options for finer-grained control that you can find in the documentation.</li> 176 </ul> 177 178 <p>A typical example of this field can be see in the edit contact activity, 179 which ensures it is resized and automatically displays the IME for the user:</p> 180 181 <pre> <activity name="EditContactActivity" 182 android:windowSoftInputMode="stateVisible|adjustResize"> 183 ... 184 </activity></pre> 185 186 <p class="note"><strong>Note:</strong>Starting from Android 1.5 (API Level 3), 187 the platform offers a new method, 188 {@link android.view.Window#setSoftInputMode(int mode)}, 189 that non-Activity windows can use to control their behavior. Calling this method 190 in your will make your application incompatible with previous versions of the 191 Android platform.</p> 192 193 <h4>Controlling the action buttons</h4> 194 195 <p>The final customization we will look at is the "action" buttons in the IME. 196 There are currently two types of actions:</p> 197 198 <ul> 199 <li>The enter key on a soft keyboard is typically bound to an action when not 200 operating on a mult-line edit text. For example, on the G1 pressing the hard 201 enter key will typically move to the next field or the application will 202 intercept it to execute an action; with a soft keyboard, this overloading of the 203 enter key remains, since the enter button just sends an enter key event.</li> 204 <li>When in fullscreen mode, an IME may also put an additional action button to 205 the right of the text being edited, giving the user quick access to a common 206 application operation.</li> 207 </ul> 208 209 <p>These options are controlled with the <code>android:imeOptions</code> 210 attribute on <code>TextView</code>. The value you supply here can be any 211 combination of:</p> 212 213 <ul> 214 <li>One of the pre-defined action constants (<code>actionGo</code>, 215 <code>actionSearch</code>, <code>actionSend</code>, <code>actionNext</code>, 216 <code>actionDone</code>). If none of these are specified, the system will infer 217 either <code>actionNext</code> or <code>actionDone</code> depending on whether 218 there is a focusable field after this one; you can explicitly force no action 219 with <code>actionNone</code>.</li> 220 <li>The <code>flagNoEnterAction</code> option tells the IME that the action 221 should <em>not</em> be available on the enter key, even if the text itself is 222 not multi-line. This avoids having unrecoverable actions like (send) that can be 223 accidentally touched by the user while typing.</li> 224 <li>The <code>flagNoAccessoryAction</code> removes the action button from the 225 text area, leaving more room for text.</li><li>The <code>flagNoExtractUi</code> 226 completely removes the text area, allowing the application to be seen behind 227 it.</li> 228 </ul> 229 230 <p>The previous IM application message view also provides an example of an 231 interesting use of <code>imeOptions</code>, to specify the send action but not 232 let it be shown on the enter key:</p> 233 234 <pre>android:imeOptions="actionSend|flagNoEnterAction"</pre> 235 236 <h3>APIs for controlling IMEs</h3> 237 238 <p>For more advanced control over the IME, there are a variety of new APIs you 239 can use. Unless special care is taken (such as by using reflection), using these 240 APIs will cause your application to be incompatible with previous versions of 241 Android, and you should make sure you specify 242 <code>android:minSdkVersion="3"</code> in your manifest. For more information, 243 see the documentation for the <a 244 href="{@docRoot}guide/topics/manifest/uses-sdk-element.html"><uses-sdk></a 245 > manifest element.</p> 246 247 <p>The primary API is the new <code>android.view.inputmethod.InputMethodManager</code> class, which you can retrieve with <code>Context.getSystemService()</code>. 248 It allows you to interact with the global input method state, such as 249 explicitly hiding or showing the current IME's input area.</p> 250 251 <p>There are also new window flags controlling input method interaction, which you can control through the existing <code>Window.addFlags()</code> method and new <code>Window.setSoftInputMode()</code> method. The <code>PopupWindow</code> 252 class has grown corresponding methods to control these options on its 253 window. One thing in particular to be aware of is the new <code>WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM</code> constant, which is used to control whether a window is on top of or behind the current IME.</p> 254 255 <p>Most of the interaction between an active IME and application is done through the <code>android.view.inputmethod.InputConnection</code> 256 class. This is the API an application implement, which an IME calls to 257 perform the appropriate edit operations on the application. You won't 258 normally need to worry about this, since <code>TextView</code> provides its own implementation for itself.</p> 259 260 <p>There are also a handful of new <code>View</code> APIs, the most important of these being<code> onCreateInputConnection()</code> which creates a new <code>InputConnection</code> for an IME (and fills in an <code>android.view.inputmethod.EditorInfo</code> 261 structure with your input type, IME options, and other data); again, 262 most developers won't need to worry about this, since TextView takes 263 care of it for you.</p>