In this article

Auto Complete

08/31/2018

4 minutes to read

Contributors

In this article

AutoCompleteTextView is an editable text view element that shows
completion suggestions automatically while the user is typing. The list
of suggestions is displayed in a drop down menu from which the user can
choose an item to replace the content of the edit box with.

Overview

To create a text entry widget that provides auto-complete suggestions,
use the
AutoCompleteTextView
widget. Suggestions are received from a collection of strings
associated with the widget through an
ArrayAdapter.

In this tutorial, you will create a
AutoCompleteTextView
widget that provides suggestions for a country name.

After the content view is set to the main.xml layout, the
AutoCompleteTextView
widget is captured from the layout with
FindViewById. A new
ArrayAdapter is then
initialized to bind the list_item.xml layout to each list item in the
COUNTRIES string array (defined in the next step). Finally,
SetAdapter() is called to associate the
ArrayAdapter with the
AutoCompleteTextView
widget so that the string array will populate the list of suggestions.

This is the list of suggestions that will be provided in a drop-down
list when the user types into the
AutoCompleteTextView
widget.

Run the application. As you type, you should see something like this:

More Information

Note that using a hard-coded string array is not a recommended design
practice because your application code should focus on behavior, not
content. Application content such as strings should be externalized
from the code to make modifications to the content easier and
facilitate localization of the content. The hard-coded strings are used
in this tutorial only to make it simple and focus on the
AutoCompleteTextView
widget. Instead, your application should declare such string arrays in
an XML file. This can be done with a <string-array> resource in your
project res/values/strings.xml file. For example: