Android AutoCompleteTextView is an advanced EditText which shows a list of suggestions when user starts typing text. When the user starts typing, a drop-down menu appears, showing a list of relevant suggestions based on the user input. The user can then select an item from the list.
The AutoCompleteTextView is a subclass of EditText class so we can easily inherit all the properties of EditText as per our requirements. The dropdown list will be obtained using the data adaptor and these suggestions appears only after entering the minimum number of characters defined in the Threshold limit. The Threshold limit is used to define the minimum number of characters the user must type to see the dropdown list of suggestions. In android, we can create an AutoCompleteTextView control in two ways either manually in an XML file or create it in the Activity file programmatically.
Different attributes of AutoCompleteText Widget
| XML Attributes | Description |
|---|---|
| android:id | Used to uniquely identify the control. |
| android:gravity | Used to specify how to align the text like left, right, center, top, etc. |
| android:text | Used to set the text. |
| android:textSize | Used to set the size of the text. |
| android:textStyle | Used to set the style of the text like bold, italic. |
| android:background | Used to set background color of the Text View. |
| android:hint | Used to set display hint text in the Text View. |
| android:maxHeight | Used to set maximum height of the Text view. |
| android:maxWidth | Used to set maximum width of the Text view. |
| android:padding | Used to set the padding from left, right, top and bottom. |
Step by Step Implementation
Step 1: Create a new project
First we create a new project by following the below steps:
- Click on File, then New > New Project.
- After that include the Kotlin support and click on next.
- Select the minimum SDK as per convenience and click next button.
- Then select the Empty activity > next > finish.
Step 2: Add the AutoCompleteTextView in activity_main.xml
In this file, we will add the AutoCompleteTextView and Button widget and set their attributes so that it can be accessed in the kotlin file.
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<AutoCompleteTextView
android:id="@+id/autoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="Please type language..."/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"/>
</LinearLayout>
Design UI:

Step 3: Access the AutoCompleteTextView in MainActivity.kt file
First of all, we declare a variable autotextview to access the widget from the XML layout.
private lateinit var autoCompleteTextView: AutoCompleteTextView
autoCompleteTextView = findViewById(R.id.autoTextView)
then, we declare another variable languages to get the items of the string-array from the strings.xml file.
private lateinit var languages: List<String>
languages = listOf("Java", "Kotlin", "Swift", "Python", "Scala", "Perl", "Javascript", "Jquery")
Create an adaptor and add into the AutoCompleteTextView of LinearLayout using
private lateinit var adapter: ArrayAdapter<String>
adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, languages)
We are familiar with further activities in previous articles like accessing button and set OnClickListener etc.
MainActivity.kt:
package org.geeksforgeeks.demo
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var autoCompleteTextView: AutoCompleteTextView
private lateinit var button: Button
private lateinit var languages: List<String>
private lateinit var adapter: ArrayAdapter<String>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
autoCompleteTextView = findViewById(R.id.autoTextView)
button = findViewById(R.id.button)
languages = listOf("Java", "Kotlin", "Swift", "Python", "Scala", "Perl", "Javascript", "Jquery")
adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, languages)
autoCompleteTextView.setAdapter(adapter)
button.setOnClickListener {
val enteredText = "Submitted language:" + " " + autoCompleteTextView.getText()
Toast.makeText(this, enteredText, Toast.LENGTH_SHORT).show()
}
}
}

