150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainpage.title=Text Fields
233baa5ad7d8cdcc89ce4fbc3bc8cd537d5f5d639Joe Fernandezpage.tags=edittext,autocompletetextview
350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main@jd:body
450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div id="qv-wrapper">
650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div id="qv">
750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  
850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2>In this document</h2>
950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<ol>
1050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li><a href="#Keyboard">Specifying the Keyboard Type</a>
1150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    <ol>
1250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main      <li><a href="#Behaviors">Controlling other behaviors</a></li>
1350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    </ol>
1450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  </li>
1550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li><a href="#Actions">Specifying Keyboard Actions</a>
1650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    <ol>
1750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main      <li><a href="#ActionEvent">Responding to action button events</a></li>
1850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main      <li><a href="#ActionLabel">Setting a custom action button label</a></li>
1950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    </ol>
2050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  </li>
2150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li><a href="#Flags">Adding Other Keyboard Flags</a></li>
2250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li><a href="#AutoComplete">Providing Auto-complete Suggestions</a></li>
2350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</ol>
2450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2>Key classes</h2>
2550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<ol>
2650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li>{@link android.widget.EditText}</li>
2750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li>{@link android.widget.AutoCompleteTextView}</li>
2850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</ol>
2950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
3050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
3150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
3250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
3350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>A text field allows the user to type text into your app. It can be either single line or
3450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainmulti-line. Touching a text field places the cursor and automatically displays the keyboard. In
3550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainaddition to typing, text fields allow for a variety of other activities, such as text selection
3650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main(cut, copy, paste) and data look-up via auto-completion.</p>
3750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
3850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>You can add a text field to you layout with the {@link android.widget.EditText} object. You
3950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainshould usually do so in your XML layout with a {@code &lt;EditText&gt;} element.</p>
4050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
4150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<img src="{@docRoot}images/ui/edittext-noextract.png" alt="" />
4250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
4350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
4450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
4550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2 id="Keyboard">Specifying the Keyboard Type</h2>
4650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
4750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div class="figure" style="width:300px;margin-top:0">
4850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<img src="{@docRoot}images/ui/edittext-text.png" alt="" />
4950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p class="img-caption"><strong>Figure 1.</strong> The default {@code text} input type.</p>
5050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
5150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
5250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div class="figure" style="width:300px">
5350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<img src="{@docRoot}images/ui/edittext-email.png" alt="" />
5450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p class="img-caption"><strong>Figure 2.</strong> The {@code textEmailAddress} input type.</p>
5550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
5650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
5750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div class="figure" style="width:300px">
5850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<img src="{@docRoot}images/ui/edittext-phone.png" alt="" />
5950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p class="img-caption"><strong>Figure 3.</strong> The {@code phone} input type.</p>
6050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
6150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
6250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Text fields can have different input types, such as number, date, password, or email address. The
6350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maintype determines what kind of characters are allowed inside the field, and may prompt the virtual
6450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainkeyboard to optimize its layout for frequently used characters.</p>
6550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
6650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>You can specify the type of keyboard you want for your {@link android.widget.EditText} object
6750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainwith the <a href="{@docRoot}reference/android/widget/TextView.html#attr_android:inputType">{@code
6850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid:inputType}</a> attribute. For example, if you want the user to input an email address, you
6950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainshould use the {@code textEmailAddress} input type:</p>
7050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
7150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
7250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;EditText
7350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:id="&#64;+id/email_address"
7450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:layout_width="fill_parent"
7550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:layout_height="wrap_content"
7650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:hint="&#64;string/email_hint"
7750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:inputType="textEmailAddress" />
7850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
7950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
8050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
81c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main<p>There are several different input types available for different situations.
82c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott MainHere are some of the more common values for
83c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main<a href="{@docRoot}reference/android/widget/TextView.html#attr_android:inputType"
84c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main>{@code android:inputType}</a>:</p>
85c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main
86c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main<dl>
87c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main  <dt>{@code "text"}</dt>
88c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main    <dd>Normal text keyboard.</dd>
89c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main  <dt>{@code "textEmailAddress"}</dt>
90c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main    <dd>Normal text keyboard with the @ character.</dd>
91c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main  <dt>{@code "textUri"}</dt>
92c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main    <dd>Normal text keyboard with the / character.</dd>
93c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main  <dt>{@code "number"}</dt>
94c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main    <dd>Basic number keypad.</dd>
95c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main  <dt>{@code "phone"}</dt>
96c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main    <dd>Phone-style keypad.</dd>
97c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main</dl>
9850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
9950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
10050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h3 id="Behaviors">Controlling other behaviors</h3>
10150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
10250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>The <a href="{@docRoot}reference/android/widget/TextView.html#attr_android:inputType">{@code
10350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid:inputType}</a> also allows you to specify certain keyboard behaviors, such as whether to
10450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maincapitalize all new words or use features like auto-complete and spelling suggestions.</p>
10550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
10650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>The <a href="{@docRoot}reference/android/widget/TextView.html#attr_android:inputType">{@code
10750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid:inputType}</a> attribute allows bitwise combinations so you can specify both a keyboard
108c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Mainlayout and one or more behaviors at once.</p>
109c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main
110c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main<p>Here are some of the common input type values that define keyboard behaviors:</p>
111c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main
112c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main<dl>
113c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main  <dt>{@code "textCapSentences"}</dt>
114c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main    <dd>Normal text keyboard that capitalizes the first letter for each new sentence.</dd>
115c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main  <dt>{@code "textCapWords"}</dt>
116c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main    <dd>Normal text keyboard that capitalizes every word. Good for titles or person names.</dd>
117c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main  <dt>{@code "textAutoCorrect"}</dt>
118c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main    <dd>Normal text keyboard that corrects commonly misspelled words.</dd>
119c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main  <dt>{@code "textPassword"}</dt>
120c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main    <dd>Normal text keyboard, but the characters entered turn into dots.</dd>
121c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main  <dt>{@code "textMultiLine"}</dt>
122c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main    <dd>Normal text keyboard that allow users to input long strings of text that include line
123c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Mainbreaks (carriage returns).</dd>
124c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main</dl>
125c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main
126c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main<p>For example, here's how you can collect a postal
12750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainaddress, capitalize each word, and disable text suggestions:</p>
12850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
12950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
13050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;EditText
13150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:id="@+id/postal_address"
13250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:layout_width="fill_parent"
13350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:layout_height="wrap_content"
13450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:hint="&#64;string/postal_address_hint"
13550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:inputType="textPostalAddress|
13650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main                       textCapWords|
13750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main                       textNoSuggestions" />
13850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
13950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
14050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>All behaviors are also listed with the <a
14150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainhref="{@docRoot}reference/android/widget/TextView.html#attr_android:inputType">{@code
14250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid:inputType}</a> documentation.</p>
14350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
14450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
14550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2 id="Actions">Specifying Keyboard Actions</h2>
14650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
14750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<div class="figure" style="width:300px">
14850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<img src="{@docRoot}images/ui/edittext-actionsend.png" alt="" />
14950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p class="img-caption"><strong>Figure 4.</strong> If you declare {@code
15050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid:imeOptions="actionSend"}, the keyboard includes the Send action.</p>
15150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</div>
15250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
15350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>In addition to changing the keyboard's input type, Android allows you to specify an action to be
15450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainmade when users have completed their input. The action specifies the button that appears in place of
15550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainthe carriage return key and the action to be made, such as "Search" or "Send."</p>
15650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
15750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>You can specify the action by setting the <a
15850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainhref="{@docRoot}reference/android/widget/TextView.html#attr_android:imeOptions">{@code
15950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid:imeOptions}</a> attribute. For example, here's how you can specify the Send action:</p>
16050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
16150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
16250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;EditText
16350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:id="@+id/search"
16450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:layout_width="fill_parent"
16550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:layout_height="wrap_content"
16650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:hint="@string/search_hint"
16750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:inputType="text"
16850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:imeOptions="actionSend" />
16950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
17050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
17150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>If you do not explicitly specify an input action then the system attempts to determine if there
17250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainare any subsequent <a
17350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainhref="{@docRoot}reference/android/view/View.html#attr_android:focusable">{@code
17450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid:focusable}</a> fields. If any focusable fields are found following this one, the system
17550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainapplies the (@code actionNext} action to the current {@link android.widget.EditText} so the user can
17650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainselect Next to move to the next field. If there's no subsequent focusable field, the system applies
17750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainthe {@code "actionDone"} action. You can override this by setting the <a
17850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainhref="{@docRoot}reference/android/widget/TextView.html#attr_android:imeOptions">{@code
17950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid:imeOptions}</a> attribute to any other value such as {@code "actionSend"} or {@code
18050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main"actionSearch"} or suppress the default behavior by using the {@code "actionNone"} action.</p>
18150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
18250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
18350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h3 id="ActionEvent">Responding to action button events</h3>
18450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
18550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>If you have specified a keyboard action for the input method using <a
18650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainhref="{@docRoot}reference/android/widget/TextView.html#attr_android:imeOptions">{@code
18750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid:imeOptions}</a> attribute (such as {@code "actionSend"}), you can listen for the specific
18850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainaction event using an {@link android.widget.TextView.OnEditorActionListener}. The {@link
18950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.widget.TextView.OnEditorActionListener} interface provides a callback method called {@link
19050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.widget.TextView.OnEditorActionListener#onEditorAction onEditorAction()} that indicates the
19150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainaction type invoked with an action ID such as {@link
19250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.view.inputmethod.EditorInfo#IME_ACTION_SEND} or {@link
19350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.view.inputmethod.EditorInfo#IME_ACTION_SEARCH}.</p>
19450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
19550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>For example, here's how you can listen for when the user clicks the Send button on the
19650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainkeyboard:</p>
19750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
19850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
19950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainEditText editText = (EditText) findViewById(R.id.search);
20050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MaineditText.setOnEditorActionListener(new OnEditorActionListener() {
20150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    &#64;Override
20250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
20350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        boolean handled = false;
20450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        if (actionId == EditorInfo.IME_ACTION_SEND) {
205c8a6f8a0414d9366bd2d3ee9d74271c8f680409bScott Main            sendMessage();
20650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main            handled = true;
20750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        }
20850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        return handled;
20950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    }
21050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main});
21150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
21250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
21350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
21450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h3 id="ActionLabel">Setting a custom action button label</h3>
21550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
21650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>If the keyboard is too large to reasonably share space with the underlying application (such as
21750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainwhen a handset device is in landscape orientation) then fullscreen ("extract mode") is triggered. In
21850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainthis mode, a labeled action button is displayed next to the input. You can customize the text of
21950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainthis button by setting the <a
22050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainhref="{@docRoot}reference/android/widget/TextView.html#attr_android:imeActionLabel">{@code
22150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid:imeActionLabel}</a> attribute:</p>
22250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
22350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
22450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;EditText
22550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:id="&#64;+id/launch_codes"
22650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:layout_width="fill_parent"
22750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:layout_height="wrap_content"
22850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:hint="&#64;string/enter_launch_codes"
22950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:inputType="number"
23050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:imeActionLabel="&#64;string/launch" />
23150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
23250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
23350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<img src="{@docRoot}images/ui/edittext-actionlabel.png" alt="" />
23450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p class="img-caption"><strong>Figure 5.</strong> A custom action label with <a
23550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainhref="{@docRoot}reference/android/widget/TextView.html#attr_android:imeActionLabel">{@code
23650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid:imeActionLabel}</a>.</p>
23750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
23850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
23950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
24050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2 id="Flags">Adding Other Keyboard Flags</h2>
24150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
24250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>In addition to the actions you can specify with the <a
24350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainhref="{@docRoot}reference/android/widget/TextView.html#attr_android:imeOptions">{@code
24450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid:imeOptions}</a> attribute, you can add additional flags to specify other keyboard
24550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainbehaviors. All available flags are listed along with the actions in the <a
24650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainhref="{@docRoot}reference/android/widget/TextView.html#attr_android:imeOptions">{@code
24750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid:imeOptions}</a> documentation.</p>
24850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
24950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>For example, figure 5 shows how the system enables a fullscreen text field when a handset device
25050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainis in landscape orientation (or the screen space is otherwise constrained for space). You can
25150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maindisable the fullscreen input mode with {@code flagNoExtractUi} in the <a
25250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainhref="{@docRoot}reference/android/widget/TextView.html#attr_android:imeOptions">{@code
25350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid:imeOptions}</a> attribute, as shown in figure 6.</p>
25450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
25550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<img src="{@docRoot}images/ui/edittext-noextract.png" alt="" />
25650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p class="img-caption"><strong>Figure 6.</strong> The fullscreen text field ("extract mode") is
25750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maindisabled with {@code android:imeOptions="flagNoExtractUi"}.</p>
25850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
25950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
26050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
26150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
26250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<h2 id="AutoComplete">Providing Auto-complete Suggestions</h2>
26350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
26450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>If you want to provide suggestions to users as they type, you can use a subclass of {@link
26550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.widget.EditText} called {@link android.widget.AutoCompleteTextView}. To implement
26650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainauto-complete, you must specify an (@link android.widget.Adapter) that provides the text
26750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainsuggestions. There are several kinds of adapters available, depending on where the data is coming
26850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainfrom, such as from a database or an array.</p>
26950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
27050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<img src="{@docRoot}images/ui/edittext-autocomplete.png" alt="" />
27150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p class="img-caption"><strong>Figure 7.</strong> Example of {@link
27250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.widget.AutoCompleteTextView} with text suggestions.</p>
27350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
27450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>The following procedure describes how to set up an {@link android.widget.AutoCompleteTextView}
27550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainthat provides suggestions from an array, using {@link android.widget.ArrayAdapter}:
27650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
27750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<ol>
27850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main  <li>Add the {@link android.widget.AutoCompleteTextView} to your layout. Here's a
27950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainlayout with only the text field:
28050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
28150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;?xml version="1.0" encoding="utf-8"?>
28250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android" 
28350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:id="&#64;+id/autocomplete_country"
28450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:layout_width="fill_parent"
28550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    android:layout_height="wrap_content" />
28650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
28750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</li>
28850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
28950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<li>Define the array that contains all text suggestions. For example, here's an array of country
29050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainnames that's defined in an XML resource file ({@code res/values/strings.xml}):
29150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
29250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;?xml version="1.0" encoding="utf-8"?>
29350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;resources>
29450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    &lt;string-array name="countries_array">
29550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        &lt;item>Afghanistan&lt;/item>
29650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        &lt;item>Albania&lt;/item>
29750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        &lt;item>Algeria&lt;/item>
29850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        &lt;item>American Samoa&lt;/item>
29950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        &lt;item>Andorra&lt;/item>
30050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        &lt;item>Angola&lt;/item>
30150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        &lt;item>Anguilla&lt;/item>
30250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        &lt;item>Antarctica&lt;/item>
30350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        ...
30450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main    &lt;/string-array>
30550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main&lt;/resources>
30650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
30750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</li>
30850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
30950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<li>In your {@link android.app.Activity} or {@link android.app.Fragment}, use the following
31050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maincode to specify the adapter that supplies the suggestions:
31150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<pre>
31250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main// Get a reference to the AutoCompleteTextView in the layout
31350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainAutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
31450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main// Get the string array
31550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainString[] countries = getResources().getStringArray(R.array.countries_array);
31650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main// Create the adapter and set it to the AutoCompleteTextView 
31750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MainArrayAdapter&lt;String> adapter = 
31850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main        new ArrayAdapter&lt;String>(this, android.R.layout.simple_list_item_1, countries);
31950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott MaintextView.setAdapter(adapter);
32050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</pre>
32150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
32250e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Here, a new {@link
32350e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainandroid.widget.ArrayAdapter} is initialized to bind each item in the <code>COUNTRIES</code>
32450e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainstring array to a {@link android.widget.TextView} that exists in the {@code simple_list_item_1}
32550e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainlayout (this is a layout provided by Android that provides a standard appearance for text in a
32650e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Mainlist).</p>
32750e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main<p>Then assign the adapter to the {@link android.widget.AutoCompleteTextView} by
32850e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Maincalling {@link android.widget.AutoCompleteTextView#setAdapter setAdapter()}.</p>
32950e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</li>
33050e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main</ol>
33150e990c64fa23ce94efa76b9e72df7f8ec3cee6aScott Main
332