English | Site Directory

Android - An Open Handset Alliance Project

android.widget
public class

android.widget.AutoCompleteTextView

java.lang.Object
android.view.View Drawable.Callback KeyEvent.Callback
android.widget.TextView
android.widget.EditText
android.widget.AutoCompleteTextView Filter.FilterListener

An editable text view 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.

The drop down can be dismissed at any time by pressing the back key or, if no item is selceted in the drop down, by pressing the enter/dpad center key.

The list of suggestions is obtained from a data adapter and appears only after a given number of charaters defined by the threshold.

The following code snippet shows how to create a text view which suggests various countries names while the user is typing:

 public class CountriesActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.countries);

         ArrayAdapter adapter = new ArrayAdapter(this,
                 android.R.layout.simple_list_item_1, COUNTRIES);
         AutoCompleteTextView textView = (AutoCompleteTextView)
                 findViewById(R.id.countries_list);
         textView.setAdapter(adapter);
     }

     private static final String[] COUNTRIES = new String[] {
         "Belgium", "France", "Italy", "Germany", "Spain"
     };
 }
 

Summary

XML Attributes

Attribute name Related methods  
android:completionHint setCompletionHint(CharSequence)
 
Defines the hint displayed in the drop down menu. 
android:completionHintSize   Defines the hint's font size. 
android:completionThreshold setThreshold(int)
 
Defines the number of characters that the user must type before completion suggestions are displayed in a drop down menu. 
android:dropDownArrowDown   Arrow down in a drop down list. 
android:dropDownArrowUp   Arrow up in a drop down list. 
android:dropDownSelector   Selector in a drop down list. 
XML Attributes inherited from class android.widget.TextView
XML Attributes inherited from class android.view.View
Constants inherited from class android.view.View
Fields inherited from class android.view.View

Public Constructors

          AutoCompleteTextView(Context context)
          AutoCompleteTextView(Context context, AttributeSet attrs, Map inflateParams)
          AutoCompleteTextView(Context context, AttributeSet attrs, Map inflateParams, int defStyle)

Public Methods

        ListAdapter  getAdapter()

Returns a filterable list adapter used for auto completion.

        OnItemClickListener  getItemClickListener()

Returns the listener that is notified whenever the user clicks an item in the drop down list.

        OnItemSelectedListener  getItemSelectedListener()

Returns the listener that is notified whenever the user selects an item in the drop down list.

        int  getThreshold()

Returns the number of characters the user must type before the drop down list is shown.

        void  onFilterComplete(int count)

Notifies the end of a filtering operation.

        boolean  onKeyDown(int keyCode, KeyEvent event)
Default implementation of KeyEvent.Callback.onKeyMultiple(): perform press of the view when KEYCODE_DPAD_CENTER or KEYCODE_NEWLINE is released, if the view is enabled and clickable.
        void  onWindowFocusChanged(boolean hasWindowFocus)
Called when the window containing this view gains or loses focus.
        void  performCompletion()

Performs the text completion by converting the selected item from the drop down list into a string, replacing the text box's content with this string and finally dismissing the drop down menu.

        void  setAdapter(T adapter)

Changes the list of data used for auto completion.

        void  setCompletionHint(CharSequence hint)

Sets the optional hint text that is displayed at the bottom of the the matching list.

        void  setOnItemClickListener(OnItemClickListener l)

Sets the listener that will be notified when the user clicks an item in the drop down list.

        void  setOnItemSelectedListener(OnItemSelectedListener l)

Sets the listener that will be notified when the user selects an item in the drop down list.

        void  setThreshold(int threshold)

Specifies the minimum number of characters the user has to type in the edit box before the drop down list is shown.

Protected Methods

        CharSequence  convertSelectionToString()

Converts the selected item from the drop down list into a sequence of character that can be used in the edit box.

        boolean  isPopupShowing()

Indicates whether the popup menu is showing.

        void  onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect)
Called by the view system when the focus state of this view changes.
        void  performFiltering(CharSequence text, int keyCode)

Starts filtering the content of the drop down list.

        void  replaceText(CharSequence text)

Performs the text completion by replacing the current text by the selected item.

        boolean  setFrame(int l, int t, int r, int b)
Assign a size and position to this view.
Methods inherited from class android.widget.EditText
Methods inherited from class android.widget.TextView