android-studio
  1. android-studio-autocompletetextview

Android Studio AutoCompleteTextView

AutoCompleteTextView is a subclass of EditText that provides auto-complete functionality to the user. It suggests a list of possible completions based on the characters entered by the user. It is a useful way to improve the user experience and speed up the input process.

Syntax

Here is the basic syntax for AutoCompleteTextView:

<AutoCompleteTextView
    android:id="@+id/autocomplete_textview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:completionThreshold="1"
    android:dropDownWidth="match_parent"
    android:dropDownVerticalOffset="wrap_content"
    android:dropDownHorizontalOffset="wrap_content"
    android:drawableEnd="@drawable/ic_search"
    android:hint="@string/search_hint"
    android:textColorHint="@color/grey_400"
    android:textColor="@color/black"
    android:textSize="16sp"/>

Example

Here is an example of how to use AutoCompleteTextView:

public class MainActivity extends AppCompatActivity {
    private AutoCompleteTextView autoCompleteTextView;
    private String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Elderberry", "Fig"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        autoCompleteTextView = findViewById(R.id.autocomplete_textview);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, fruits);
        autoCompleteTextView.setAdapter(adapter);
    }
}

Output

AutoCompleteTextView Example Output

Explanation

In this example, we have created an AutoCompleteTextView and set an adapter to it. The adapter is an instance of ArrayAdapter that takes an array of strings containing the possible suggestions.

The completionThreshold attribute is set to 1, which means that the suggestions will start showing up after the user types one character.

The dropDownWidth attribute is set to match_parent, which means that the suggestions dropdown will take up the full width of the AutoCompleteTextView.

The drawableEnd attribute is set to an icon that will appear at the end of the AutoCompleteTextView.

The hint attribute and the textColorHint attribute define the hint text and its color respectively.

The textColor attribute sets the text color of the input text.

The textSize attribute sets the size of the input text.

Use

AutoCompleteTextView can be used in any app where users need to enter text that can be completed automatically. Some use cases include searching for items, suggesting tags or categories, autocompleting email addresses, or creating accounts.

Important Points

  • AutoCompleteTextView is a subclass of EditText that provides auto-complete functionality.
  • An adapter needs to be set to the AutoCompleteTextView to provide the list of suggestions.
  • The completionThreshold attribute defines the minimum number of characters required to show suggestions.
  • The hint attribute provides a hint text for the user to enter text.
  • The textSize attribute sets the size of the input text.
  • The dropDownWidth attribute sets the width of the dropdown list.
  • The drawableEnd attribute is used to provide an icon next to the input text.

Summary

AutoCompleteTextView is a useful component in Android that provides auto-complete functionality to the user. By setting an adapter to it, it suggests a list of possible completions based on the characters entered by the user. With its various attributes, it provides a rich user experience and speed up the input process.

Published on: