Introduction
In this blog, we will look into the AutoComplete Feature of Android. AutoComplete feature gives suggestions while writing in a text box. We will discuss how AutoComplete is created and registered. Additionally, we will look at the different kinds of attributes an AutoComplete can have and their uses.
In this article, we will be using the Android Studio application, so in case you haven't yet set up the application on your system, you can check out this article.
AutoComplete
AutoCompleteTextView provides suggestions as you type in an editable text field. When the user is typing, it automatically displays a list of suggestions. A drop-down menu of options appears, from which the user can select an item to replace the content of the edit box with.
The AutoCompleteTextView tag is used to create an AutoCompleteTextView in an android application. For example,
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginStart="94dp"
android:layout_marginTop="292dp"
android:text=""
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Attributes of AutoCompleteTextView
Following are some of the important attributes of an AutoCompleteTextView:
- android:id: A Unique id to identify any widget
- android:layout_width: It defines the width of the layout
- android:layout_height: It sets the layout's height
- android:layout_marginStart: The amount of space needed at the start of the widget
- android:layout_marginTop: It represents the amount of space needed on the top of the widget
- android:text: Specifies the default text displayed in the field
- app:layout_constraintTop_toTopOf: Align the desired view's top with the top of another
Example Application
Now that we have learned what an AutoCompleteTextView is. Let's learn how to code it in a program by building a basic Android application that uses an AutoCompleteTextView.
First, we need to create a new project in Android Studio by selecting the empty activity option. Now, we will add the add an AutoCompleteTextView inside the activity_main.xml file.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginStart="94dp"
android:layout_marginTop="292dp"
android:text=""
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now go to the MainActivity.kt file and set the array that needs to be displayed in the drop-down menu.
MainActivity.kt
package com.example.androidautocompletefeature
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
// Main activity extending AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var autoCompleteTextView: AutoCompleteTextView
private var threshold = 0 // initialising threshold with zero
// array of colors that will be displayed in the drop down menu
var colors = arrayOf("Red", "Blue", "Green", "Black", "Orange","Violet")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// fetch the Auto Complete Text View
autoCompleteTextView = findViewById<AutoCompleteTextView>(R.id.autoCompleteTextView)
// initializing the adapter
val adapter: ArrayAdapter<*> = ArrayAdapter<Any?>(this, android.R.layout.simple_list_item_1, colors)
// set the adapter
autoCompleteTextView!!.setAdapter(adapter)
threshold = 2 // set threshold to two character
// setting the threshold
autoCompleteTextView!!.setThreshold(threshold)
}
}
Output:
As you can see from the output, upon typing "Bl" in the text field, it automatically drops down a list of suggestions.




