AutoCompleteTextView Android

To complete the words based on the reserved words, the Android AutoCompleteTextView is used in Android. Thus it is not required to write all the characters of the word. It is an editable text field. A list of suggestions is thus displayed in a drop-down menu. At a time only one suggestion or value can be selected by the user from the drop-down menu. The EditText class has a subclass named AutoCompleteTextView and the AutoCompleteTextView class has a subclass named MultiAutoCompleteTextView.

Android AutoCompleteTextView Example:

We are here demonstrating an example to display the Gender options in the AutoCompleteTextView in Android. A string array is used to store all the Gender options. And to display the array content, the ArrayAdapter class is used.

activity_main.xml:

In the activity_main.xml file, drag the AutoCompleteTextView and TextView from the pallet.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="MainActivity">
 
   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Which is your favourite Fruit?"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       app:layout_constraintVertical_bias="0.032" />
 
   <AutoCompleteTextView
       android:id="@+id/autoCompleteTextView"
       android:layout_width="200dp"
       android:layout_height="wrap_content"
       android:layout_marginLeft="92dp"
       android:layout_marginTop="144dp"
       android:text=""
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent" />
 
</android.support.constraint.ConstraintLayout>

Activity class:(File: MainActivity.java)

The code of AutoCompleteTextView is written in the MainActivity.java file.

package com.example.radioapp;
 
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
 
public class MainActivity extends AppCompatActivity {
   String[] fruits ={"Apple","Banana","Papaya","Grapes","Orange","Pineapple","Guava"};
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       //Creating the instance of ArrayAdapter containing list of fruits names
       ArrayAdapter<String> adapter = new ArrayAdapter<String>
               (this,android.R.layout.select_dialog_item,fruits);
       //Getting the instance of AutoCompleteTextView
       AutoCompleteTextView actv =  (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
       actv.setThreshold(1);//will start working from first character
       actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
       actv.setTextColor(Color.RED);
   }
}

Output 1:

Output 2: