Open In App
Related Articles

Dynamic AutoCompleteTextView in Kotlin

Like Article
Save Article
Report issue

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 threshold limit and user can choose an item from 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 data adaptor and these suggestions will be appeared only after entering the minimum number 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 a AutoCompleteTextView control in two ways either manually in XML file or create it in 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.

Use LinearLayout in activity_main.xml file

In this file, we only use the LinearLayout and set it attributes. 


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


Create list of suggestions in strings.xml file

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">DynamicAutoCompleteTextView</string>
    <string name="hint">Please type language...</string>
    <string name="submit">Submit</string>
    <string name="submitted_lang">Submitted language:</string>
    <string-array name="Languages">


Create AutoCompleteTextView and button in MainActivity.kt file

First of all, we declare two variables autotextview and button to create the widgets and set their attributes.

val autotextView = AutoCompleteTextView(this)
val button = Button(this)

and add both autotextview and button in LinearLayout using

val linearLayout = findViewById(
   // Add AutoCompleteTextView and button to LinearLayout

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)


package com.geeksforgeeks.myfirstkotlinapp
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.*
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        //create AutoCompleteTextView and button
        val autotextView = AutoCompleteTextView(this)
        val button = Button(this)
        val layoutParams = LinearLayout.LayoutParams(
        autotextView.layoutParams = layoutParams
        button.layoutParams = layoutParams
        layoutParams.setMargins(30, 30, 30, 30)
        val linearLayout = findViewById<LinearLayout>(
        // Add AutoCompleteTextView and button to LinearLayout
        // 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)
        if (button != null) {
            button?.setOnClickListener(View.OnClickListener {
                val enteredText = getString(R.string.submitted_lang)+ " " +
                    enteredText, Toast.LENGTH_SHORT).show()


AndroidManifest.xml file


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


Run as Emulator:

Last Updated : 21 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Similar Reads