Last Updated:

AutoCompleteTextView in Android - Examples

In this article, we'll learn how to get hints using the . As usual, let's look at a simple example.

AutoCompleteTextView

AutoCompleteTextView widget in Android. Theory

The widget shows tooltips as the user enters text. A list of these tooltips is displayed as a drop-down menu from which the user can select the appropriate tooltip. A set of all possible hints is provided by the adapter and appears only after the user enters a few characters.

AutoCompleteTextView

In an XML layout, a widget is defined as follows:

Note the . It sets the width of the widget regardless of the actual size of the text you enter.android:emsTextView

The most useful methods when working with a widget in code are:AutoCompleteTextView

  • The method installs the adapter in the widget.setAdapter()AutoCompleteTextView
  • the method returns the adapter that is used to automatically provide hintsgetAdapter()
  • The method returns the text displayed at the bottom of the list of tooltips.getCompletionHint()
  • The method returns the ID of the View to which the drop-down list will fall.getDropDownAnchor()
  • The method returns the position of the item from the drop-down list.getListSelection()
  • The method determines whether the pop-up menu is shown or notisPopupShowing()
  • The method sets the text in the widget with the ability to disable filteringsetText(CharSequence text, boolean filter)
  • The method shows a drop-down listshowDropDown()

AutoCompleteTextView widget in Android. Example

In our example, we use the widget in the RelativeLayout layout as well. In the code, we will use with a list of any elements. For simplicity in the example, we'll use the back-of-the-clock data in the array. In a real application, this data will be provided from a database or from some Internet service.

TextViewAutoCompleteTextViewArrayAdapter

The XML layout file is shown below:

The following is a listing of the class:MainActivity

In the above code, we saved the list in with the standard Android layout. Notice that we used the method to indicate the need for a list with hints after entering only one character.ArrayAdaptersetThreshold(1)

Note that the list with tooltips is only visible when the input field has gained focus.

 

Below is the result of using the widget in Android:AutoCompleteTextView

AutoCompleteTextView in Android