Home | History | Annotate | Download | only in controls
      1 page.title=Text Fields
      2 parent.title=Input Controls
      3 parent.link=../controls.html
      4 @jd:body
      5 
      6 <div id="qv-wrapper">
      7 <div id="qv">
      8   
      9 <h2>In this document</h2>
     10 <ol>
     11   <li><a href="#Keyboard">Specifying the Keyboard Type</a>
     12     <ol>
     13       <li><a href="#Behaviors">Controlling other behaviors</a></li>
     14     </ol>
     15   </li>
     16   <li><a href="#Actions">Specifying Keyboard Actions</a>
     17     <ol>
     18       <li><a href="#ActionEvent">Responding to action button events</a></li>
     19       <li><a href="#ActionLabel">Setting a custom action button label</a></li>
     20     </ol>
     21   </li>
     22   <li><a href="#Flags">Adding Other Keyboard Flags</a></li>
     23   <li><a href="#AutoComplete">Providing Auto-complete Suggestions</a></li>
     24 </ol>
     25 <h2>Key classes</h2>
     26 <ol>
     27   <li>{@link android.widget.EditText}</li>
     28   <li>{@link android.widget.AutoCompleteTextView}</li>
     29 </ol>
     30 
     31 </div>
     32 </div>
     33 
     34 <p>A text field allows the user to type text into your app. It can be either single line or
     35 multi-line. Touching a text field places the cursor and automatically displays the keyboard. In
     36 addition to typing, text fields allow for a variety of other activities, such as text selection
     37 (cut, copy, paste) and data look-up via auto-completion.</p>
     38 
     39 <p>You can add a text field to you layout with the {@link android.widget.EditText} object. You
     40 should usually do so in your XML layout with a {@code &lt;EditText&gt;} element.</p>
     41 
     42 <img src="{@docRoot}images/ui/edittext-noextract.png" alt="" />
     43 
     44 
     45 
     46 <h2 id="Keyboard">Specifying the Keyboard Type</h2>
     47 
     48 <div class="figure" style="width:300px;margin-top:0">
     49 <img src="{@docRoot}images/ui/edittext-text.png" alt="" />
     50 <p class="img-caption"><strong>Figure 1.</strong> The default {@code text} input type.</p>
     51 </div>
     52 
     53 <div class="figure" style="width:300px">
     54 <img src="{@docRoot}images/ui/edittext-email.png" alt="" />
     55 <p class="img-caption"><strong>Figure 2.</strong> The {@code textEmailAddress} input type.</p>
     56 </div>
     57 
     58 <div class="figure" style="width:300px">
     59 <img src="{@docRoot}images/ui/edittext-phone.png" alt="" />
     60 <p class="img-caption"><strong>Figure 3.</strong> The {@code phone} input type.</p>
     61 </div>
     62 
     63 <p>Text fields can have different input types, such as number, date, password, or email address. The
     64 type determines what kind of characters are allowed inside the field, and may prompt the virtual
     65 keyboard to optimize its layout for frequently used characters.</p>
     66 
     67 <p>You can specify the type of keyboard you want for your {@link android.widget.EditText} object
     68 with the <a href="{@docRoot}reference/android/widget/TextView.html#attr_android:inputType">{@code
     69 android:inputType}</a> attribute. For example, if you want the user to input an email address, you
     70 should use the {@code textEmailAddress} input type:</p>
     71 
     72 <pre>
     73 &lt;EditText
     74     android:id="&#64;+id/email_address"
     75     android:layout_width="fill_parent"
     76     android:layout_height="wrap_content"
     77     android:hint="&#64;string/email_hint"
     78     android:inputType="textEmailAddress" />
     79 </pre>
     80 
     81 
     82 <p>There are several different input types available for different situations. You can find
     83 them all listed with the documentation for <a
     84 href="{@docRoot}reference/android/widget/TextView.html#attr_android:inputType">{@code
     85 android:inputType}</a>.</p>
     86 
     87 <p class="note"><strong>Tip:</strong> To allow users to input long strings of text with line
     88 breaks, use the {@code "textMultiLine"} input type. By default, an {@link android.widget.EditText}
     89 object is restricted to one line of text and scrolls horizontally when the text exceeds the
     90 available width.</p>
     91 
     92 
     93 <h3 id="Behaviors">Controlling other behaviors</h3>
     94 
     95 <p>The <a href="{@docRoot}reference/android/widget/TextView.html#attr_android:inputType">{@code
     96 android:inputType}</a> also allows you to specify certain keyboard behaviors, such as whether to
     97 capitalize all new words or use features like auto-complete and spelling suggestions.</p>
     98 
     99 <p>The <a href="{@docRoot}reference/android/widget/TextView.html#attr_android:inputType">{@code
    100 android:inputType}</a> attribute allows bitwise combinations so you can specify both a keyboard
    101 layout and one or more behaviors at once. For example, here's how you can collect a postal
    102 address, capitalize each word, and disable text suggestions:</p>
    103 
    104 <pre>
    105 &lt;EditText
    106     android:id="@+id/postal_address"
    107     android:layout_width="fill_parent"
    108     android:layout_height="wrap_content"
    109     android:hint="&#64;string/postal_address_hint"
    110     android:inputType="textPostalAddress|
    111                        textCapWords|
    112                        textNoSuggestions" />
    113 </pre>
    114 
    115 <p>All behaviors are also listed with the <a
    116 href="{@docRoot}reference/android/widget/TextView.html#attr_android:inputType">{@code
    117 android:inputType}</a> documentation.</p>
    118 
    119 
    120 <h2 id="Actions">Specifying Keyboard Actions</h2>
    121 
    122 <div class="figure" style="width:300px">
    123 <img src="{@docRoot}images/ui/edittext-actionsend.png" alt="" />
    124 <p class="img-caption"><strong>Figure 4.</strong> If you declare {@code
    125 android:imeOptions="actionSend"}, the keyboard includes the Send action.</p>
    126 </div>
    127 
    128 <p>In addition to changing the keyboard's input type, Android allows you to specify an action to be
    129 made when users have completed their input. The action specifies the button that appears in place of
    130 the carriage return key and the action to be made, such as "Search" or "Send."</p>
    131 
    132 <p>You can specify the action by setting the <a
    133 href="{@docRoot}reference/android/widget/TextView.html#attr_android:imeOptions">{@code
    134 android:imeOptions}</a> attribute. For example, here's how you can specify the Send action:</p>
    135 
    136 <pre>
    137 &lt;EditText
    138     android:id="@+id/search"
    139     android:layout_width="fill_parent"
    140     android:layout_height="wrap_content"
    141     android:hint="@string/search_hint"
    142     android:inputType="text"
    143     android:imeOptions="actionSend" />
    144 </pre>
    145 
    146 <p>If you do not explicitly specify an input action then the system attempts to determine if there
    147 are any subsequent <a
    148 href="{@docRoot}reference/android/view/View.html#attr_android:focusable">{@code
    149 android:focusable}</a> fields. If any focusable fields are found following this one, the system
    150 applies the (@code actionNext} action to the current {@link android.widget.EditText} so the user can
    151 select Next to move to the next field. If there's no subsequent focusable field, the system applies
    152 the {@code "actionDone"} action. You can override this by setting the <a
    153 href="{@docRoot}reference/android/widget/TextView.html#attr_android:imeOptions">{@code
    154 android:imeOptions}</a> attribute to any other value such as {@code "actionSend"} or {@code
    155 "actionSearch"} or suppress the default behavior by using the {@code "actionNone"} action.</p>
    156 
    157 
    158 <h3 id="ActionEvent">Responding to action button events</h3>
    159 
    160 <p>If you have specified a keyboard action for the input method using <a
    161 href="{@docRoot}reference/android/widget/TextView.html#attr_android:imeOptions">{@code
    162 android:imeOptions}</a> attribute (such as {@code "actionSend"}), you can listen for the specific
    163 action event using an {@link android.widget.TextView.OnEditorActionListener}. The {@link
    164 android.widget.TextView.OnEditorActionListener} interface provides a callback method called {@link
    165 android.widget.TextView.OnEditorActionListener#onEditorAction onEditorAction()} that indicates the
    166 action type invoked with an action ID such as {@link
    167 android.view.inputmethod.EditorInfo#IME_ACTION_SEND} or {@link
    168 android.view.inputmethod.EditorInfo#IME_ACTION_SEARCH}.</p>
    169 
    170 <p>For example, here's how you can listen for when the user clicks the Send button on the
    171 keyboard:</p>
    172 
    173 <pre>
    174 EditText editText = (EditText) findViewById(R.id.search);
    175 editText.setOnEditorActionListener(new OnEditorActionListener() {
    176     &#64;Override
    177     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    178         boolean handled = false;
    179         if (actionId == EditorInfo.IME_ACTION_SEND) {
    180             // Send the user message
    181             handled = true;
    182         }
    183         return handled;
    184     }
    185 });
    186 </pre>
    187 
    188 
    189 <h3 id="ActionLabel">Setting a custom action button label</h3>
    190 
    191 <p>If the keyboard is too large to reasonably share space with the underlying application (such as
    192 when a handset device is in landscape orientation) then fullscreen ("extract mode") is triggered. In
    193 this mode, a labeled action button is displayed next to the input. You can customize the text of
    194 this button by setting the <a
    195 href="{@docRoot}reference/android/widget/TextView.html#attr_android:imeActionLabel">{@code
    196 android:imeActionLabel}</a> attribute:</p>
    197 
    198 <pre>
    199 &lt;EditText
    200     android:id="&#64;+id/launch_codes"
    201     android:layout_width="fill_parent"
    202     android:layout_height="wrap_content"
    203     android:hint="&#64;string/enter_launch_codes"
    204     android:inputType="number"
    205     android:imeActionLabel="&#64;string/launch" />
    206 </pre>
    207 
    208 <img src="{@docRoot}images/ui/edittext-actionlabel.png" alt="" />
    209 <p class="img-caption"><strong>Figure 5.</strong> A custom action label with <a
    210 href="{@docRoot}reference/android/widget/TextView.html#attr_android:imeActionLabel">{@code
    211 android:imeActionLabel}</a>.</p>
    212 
    213 
    214 
    215 <h2 id="Flags">Adding Other Keyboard Flags</h2>
    216 
    217 <p>In addition to the actions you can specify with the <a
    218 href="{@docRoot}reference/android/widget/TextView.html#attr_android:imeOptions">{@code
    219 android:imeOptions}</a> attribute, you can add additional flags to specify other keyboard
    220 behaviors. All available flags are listed along with the actions in the <a
    221 href="{@docRoot}reference/android/widget/TextView.html#attr_android:imeOptions">{@code
    222 android:imeOptions}</a> documentation.</p>
    223 
    224 <p>For example, figure 5 shows how the system enables a fullscreen text field when a handset device
    225 is in landscape orientation (or the screen space is otherwise constrained for space). You can
    226 disable the fullscreen input mode with {@code flagNoExtractUi} in the <a
    227 href="{@docRoot}reference/android/widget/TextView.html#attr_android:imeOptions">{@code
    228 android:imeOptions}</a> attribute, as shown in figure 6.</p>
    229 
    230 <img src="{@docRoot}images/ui/edittext-noextract.png" alt="" />
    231 <p class="img-caption"><strong>Figure 6.</strong> The fullscreen text field ("extract mode") is
    232 disabled with {@code android:imeOptions="flagNoExtractUi"}.</p>
    233 
    234 
    235 
    236 
    237 <h2 id="AutoComplete">Providing Auto-complete Suggestions</h2>
    238 
    239 <p>If you want to provide suggestions to users as they type, you can use a subclass of {@link
    240 android.widget.EditText} called {@link android.widget.AutoCompleteTextView}. To implement
    241 auto-complete, you must specify an (@link android.widget.Adapter) that provides the text
    242 suggestions. There are several kinds of adapters available, depending on where the data is coming
    243 from, such as from a database or an array.</p>
    244 
    245 <img src="{@docRoot}images/ui/edittext-autocomplete.png" alt="" />
    246 <p class="img-caption"><strong>Figure 7.</strong> Example of {@link
    247 android.widget.AutoCompleteTextView} with text suggestions.</p>
    248 
    249 <p>The following procedure describes how to set up an {@link android.widget.AutoCompleteTextView}
    250 that provides suggestions from an array, using {@link android.widget.ArrayAdapter}:
    251 
    252 <ol>
    253   <li>Add the {@link android.widget.AutoCompleteTextView} to your layout. Here's a
    254 layout with only the text field:
    255 <pre>
    256 &lt;?xml version="1.0" encoding="utf-8"?>
    257 &lt;AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    258     android:id="&#64;+id/autocomplete_country"
    259     android:layout_width="fill_parent"
    260     android:layout_height="wrap_content" />
    261 </pre>
    262 </li>
    263 
    264 <li>Define the array that contains all text suggestions. For example, here's an array of country
    265 names that's defined in an XML resource file ({@code res/values/strings.xml}):
    266 <pre>
    267 &lt;?xml version="1.0" encoding="utf-8"?>
    268 &lt;resources>
    269     &lt;string-array name="countries_array">
    270         &lt;item>Afghanistan&lt;/item>
    271         &lt;item>Albania&lt;/item>
    272         &lt;item>Algeria&lt;/item>
    273         &lt;item>American Samoa&lt;/item>
    274         &lt;item>Andorra&lt;/item>
    275         &lt;item>Angola&lt;/item>
    276         &lt;item>Anguilla&lt;/item>
    277         &lt;item>Antarctica&lt;/item>
    278         ...
    279     &lt;/string-array>
    280 &lt;/resources>
    281 </pre>
    282 </li>
    283 
    284 <li>In your {@link android.app.Activity} or {@link android.app.Fragment}, use the following
    285 code to specify the adapter that supplies the suggestions:
    286 <pre>
    287 // Get a reference to the AutoCompleteTextView in the layout
    288 AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
    289 // Get the string array
    290 String[] countries = getResources().getStringArray(R.array.countries_array);
    291 // Create the adapter and set it to the AutoCompleteTextView 
    292 ArrayAdapter&lt;String> adapter = 
    293         new ArrayAdapter&lt;String>(this, android.R.layout.simple_list_item_1, countries);
    294 textView.setAdapter(adapter);
    295 </pre>
    296 
    297 <p>Here, a new {@link
    298 android.widget.ArrayAdapter} is initialized to bind each item in the <code>COUNTRIES</code>
    299 string array to a {@link android.widget.TextView} that exists in the {@code simple_list_item_1}
    300 layout (this is a layout provided by Android that provides a standard appearance for text in a
    301 list).</p>
    302 <p>Then assign the adapter to the {@link android.widget.AutoCompleteTextView} by
    303 calling {@link android.widget.AutoCompleteTextView#setAdapter setAdapter()}.</p>
    304 </li>
    305 </ol>
    306 
    307