Fields Now With 35 Less Typing

The AutoCompleteTextview is sort of a hybrid between the Editview (field) and the Spinner. With auto-completion, as the user types, the text is treated as a prefix filter, comparing the entered text as a prefix against a list of candidates. Matches are shown in a selection list that, like with Spinner, folds down from the field. The user can either type out an entry (e.g., something not in the list) or choose an entry from the list to be the value of the field.

AutoCompleteTextview subclasses Editview, so you can configure all the standard look-and-feel aspects, such as font face and color.

In addition, AutoCompleteTextview has a android:completionThreshold property, to indicate the minimum number of characters a user must enter before the list filtering begins.

You can give AutoCompleteTextview an adapter containing the list of candidate values via setAdapter(). However, since the user could type

something not in the list, AutoCompleteTextView does not support selection listeners. Instead, you can register a TextWatcher, like you can with any EditView, to be notified when the text changes. These events will occur either because of manual typing or from a selection from the drop-down list.

Below we have a familiar-looking XML layout, this time containing an AutoCompleteTextView (pulled from the AutoComplete sample application):

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<TextView android:id="@+id/selection" android:layout_width="fill_parent" android:layout_height="wrap_content" />

<AutoCompleteTextView android:id="@+id/edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:completionThreshold="3"/> </LinearLayout>

The corresponding Java code is:

public class AutoCompleteDemo extends Activity implements TextWatcher {

TextView selection;

AutoCompleteTextView edit;

String[] items={"lorem", "ipsum", "dolor", "sit", "amet",

"consectetuer", "adipiscing", "elit", "morbi", "vel", "ligula", "vitae", "arcu", "aliquet", "mollis", "etiam", "vel", "erat", "placerat", "ante", "porttitor", "sodales", "pellentesque", "augue", "purus"};

^Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);

selection=(TextView)findViewById(; edit=(AutoCompleteTextView)findViewById(R. id.edit); edit.addTextChangedListener(this);

edit.setAdapter(new ArrayAdapter<String>(this,

android.R.layout.simple_list_item_1, items));

public void onTextChanged(CharSequence s, int start, int before, int count) { selection.setText(edit.getText());

public void beforeTextChanged(CharSequence s, int start, int count, int after) { // needed for interface, but not used

This time, our activity implements TextWatcher, which means our callbacks are onTextChanged() and beforeTextChanged(). In this case, we are only interested in the former, and we update the selection label to match the AutoCompleteTextView's current contents.

Here we have the results:

Figure 22. The AutoCompleteDemo sample application, as initially launched

AutoCompleteDemo lor

AutoCompleteDemo lor

Figure 23. The same application, after a few matching letters were entered, showing the auto-complete drop-down
Figure 24. The same application, after the auto-complete value was selected
Self Publishing

Self Publishing

Have you always wanted to write your own book and get it published? Discover How to Write, Print Sell Your Own Book! Have you always wanted to write your own book and get it published? If you have written a book or even if you have a yen to be a writer, you are probably already aware of the competition in the writing field.

Get My Free Ebook

Post a comment