Skip to content
Related Articles
Open in App
Not now

Related Articles

AutoCompleteTextView in Kotlin

Improve Article
Save Article
  • Last Updated : 28 Mar, 2022
Improve Article
Save Article

Android AutoCompleteTextView is an editable text view which shows a list of suggestions when user starts typing text. When a user starts typing, a dropdown menu will be there based on the entered characters, defined in the threshold limit and the user can choose an item from the list to replace the text.

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 will be appeared 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.

First we create a new project by following the below steps:

  1. Click on File, then New => New Project.
  2. After that include the Kotlin support and click on next.
  3. Select the minimum SDK as per convenience and click next button.
  4. Then select the Empty activity => next => finish.

Different attributes of AutoCompleteText widget –

XML AttributesDescription
android:idUsed to uniquely identify the control.
android:gravityUsed to specify how to align the text like left, right, center, top, etc.
android:textUsed to set the text.
android:textSizeUsed to set the size of the text.
android:textStyleUsed to set the style of the text like bold, italic.
android:backgroundUsed to set background color of the Text View.
android:hintUsed to set display hint text in the Text View.
android:maxHeightUsed to set maximum height of the Text view.
android:maxWidthUsed to set maximum width of the Text view.
android:paddingUsed to set the padding from left, right, top and bottom.

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.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://"

Modify the strings.xml file to add the string-array

Here, we will specify the name of the activity and define other strings which can be used at different places in our activity. Another important thing is that we will define string_array which contains the items for the suggestion list of AutoCompleteTextView.


    <string name="app_name">AutoCompleteTextViewInKotlin</string>
    <string name="hint">Please type language...</string>
    <string name="submit">Submit</string>
    <string name="submitted_lang">Submitted language:</string>
    <string-array name="Languages">

Access the AutoCompleteTextView in MainActivity.kt file

First of all, we declare a variable autotextview to access the widget from the XML layout.

val autotextView = findViewById<AutoCompleteTextView>(

then, we declare another variable languages to get the items of the string-array from the strings.xml file.

val languages = resources.getStringArray(R.array.Languages)

Create an adaptor and add into the AutoCompleteTextView of LinearLayout using

val 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.


package com.geeksforgeeks.myfirstkotlinapp
    import android.os.Bundle import android.view.View import import android.widget.ArrayAdapter import android.widget.AutoCompleteTextView import android.widget.Button import android.widget.Toast
    class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?)
                val autotextView
            = findViewById<AutoCompleteTextView>(
            // Get the array of languages
            val languages
            = resources.getStringArray(R.array.Languages)
              // Create adapter and add in AutoCompleteTextView
              val adapter
            = ArrayAdapter(this,
                           android.R.layout.simple_list_item_1, languages)
                      val button
            = findViewById<Button>( if (button != null)
            button ?.setOnClickListener(View.OnClickListener {
                        val enteredText = getString(R.string.submitted_lang) + " " + autotextView.getText() Toast.makeText(this @MainActivity, enteredText, Toast.LENGTH_SHORT).show()

AndroidManifest.xml file


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://"
    <activity android:name=".MainActivity">
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

Run as Emulator:


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!