Auto CompleteText View

The AutoCompleteTextView control is a TextView with auto-complete functionality. In other words, as the user types in the TextView, the control can display suggestions for the user to select. Listing 4-7 demonstrates the AutoCompleteTextView control.

Listing 4-7. Using an AutoCompleteTextView Control

AutoCompleteTextView actv = (AutoCompleteTextView) this.findViewByld(;

ArrayAdapter<String> aa = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, new String[] {"English", "Hebrew", "Hindi", "Spanish", "German","Greek" });


The AutoCompleteTextView control shown in Listing 4-7 suggests a language to the user. For example, if the user types en, the control suggests English. If the user types gr, the control recommends Greek, and so on.

If you have used a suggestion control, or a similar auto-complete control, then you know that controls like this have two parts: a text-view control and a control that displays the suggestion(s). That's the general concept. To use a control like this, you have to create the control, create the list of suggestions, tell the control the list of suggestions, and possibly tell the control how to display the suggestions. Alternatively, you could create a second control for the suggestions and then associate the two controls.

Android has made this simple, as is evident from Listing 4-7. To use an AutoCompleteTextView, you can define the control in your layout file and then reference it in your activity. You then create an adapter class that holds the suggestions and define the ID of the control that will show the suggestion (in this case, a simple list item). In Listing 4-7, the second parameter to the ArrayAdapter tells the adapter to use a simple list item to show the suggestion. The final step is to associate the adapter with the AutoCompleteTextView, which you do using the setAdapter() method.

0 0

Post a comment