Android AutoCompleteTextview Using Kotlin With Example

In this article, we will learn about android AutoCompleteTextView using Kotlin. We will go through various example that demonstrates how to use different attributes of AutoCompleteTextView. For example,

In this article, we will get answer to questions like –

  • What is AutoCompleteTextView?
  • Why should we consider AutoCompleteTextView while designing ui for any app?
  • What are possibilities using AutoCompleteTextView while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Android AutoCompletetTextView Output Android AutoCompleteTextView Using Kotlin

Tutorialwing Android AutoCompletetTextView Output

Getting Started

We can define android AutoCompleteTextView widget as below –

AutoCompleteTextView is an editable textView that shows completion suggestions while user is typing in the view i.e. When user starts typing in the view, as soon as entered character crosses thresold character limit, user sees some available suggestions based on the entered character.

Now, how do we use AutoCompleteTextView in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as AutoCompleteTextView. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android AutoCompleteTextView Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using AutoCompleteTextView in Kotlin

Follow steps below to use AutoCompleteTextView in newly created project –

  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">AutoCompleteTextView</string>
        <string name="hint">Please type something...</string>
        <string name="submit">Submit</string>
        <string name="entered_text">Entered text:</string>
    
        <string-array name="countries_array">
            <item>India</item>
            <item>Albania</item>
            <item>Algeria</item>
            <item>American Samoa</item>
            <item>Andorra</item>
            <item>Angola</item>
            <item>Anguilla</item>
            <item>Antarctica</item>
        </string-array>
    
    </resources>
    

    In strings.xml file, we have also defined an array (i.e. countries_array) that will act as suggestions list to show when user starts typing in the autoCompleteTextview.

  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?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="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:hint="@string/hint"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/btnSubmit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/submit"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/autoCompleteTextView" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    In activity_main.xml file, we have defined AutoCompleteTextView. Now, we will access this autoCompleteTextview using kotlin file to perform some operations on it.

  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.autocompletetextview
    
    import android.os.Bundle
    import android.widget.ArrayAdapter
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import com.tutorialwing.autocompletetextview.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		val view = binding.root
    		setContentView(view)
    
    		setupAutoCompleteTextView()
    	}
    
    	private fun setupAutoCompleteTextView() {
    		val countries = resources.getStringArray(R.array.countries_array)
    		// Create the adapter and set it to the AutoCompleteTextView
    		val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, countries)
    		binding.autoCompleteTextView.setAdapter(adapter)
    
    		binding.btnSubmit.setOnClickListener {
    			val text = getString(R.string.entered_text) + " " + binding.autoCompleteTextView.text
    			Toast.makeText(this@MainActivity, text, Toast.LENGTH_SHORT).show()
    		}
    	}
    }
    

    In MainActivity.kt file, we have accessed AutoCompleteTextView defined in xml file. Then, we have defined an adapter using the array (i.e. countries_array) defined in strings.xml. After that, we have set this adapter in AutoCompleteTextView. This adapter is responsible to display completion suggestions as list in the dropdown of autoCompletion while user is typing in the view. At last, we have set click listener to button, defined in xml file, to display a toast message, containing the text entered in autoCompleteTextView.

Now, run the application. We will get output as below –

Tutorialwing Android AutoCompletetTextView Output Android AutoCompleteTextView Using Kotlin

Tutorialwing Android AutoCompletetTextView Output

Different Attributes of AutoCompleteTextView in XML

Now, we will see how to use different attributes of Android AutoCompleteTextView using Kotlin to customise it –

Set Id of AutoCompleteTextView

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of AutoCompleteTextView using android:id attribute like below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        />

Here, we have set id of AutoCompleteTextView as autoCompleteTextView_ID using android:id=”” attribute. So, if we need to reference this AutoCompleteTextView, we need to use this id – autoCompleteTextView_ID.
Learn to Set ID of AutoCompleteTextView Dynamically

Set Width of AutoCompleteTextView

We use android:layout_width=”” attribute to set width of AutoCompleteTextView.
We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of AutoCompleteTextView Dynamically

Set Height of AutoCompleteTextView

We use android:layout_height=”” attribute to set height of AutoCompleteTextView.
We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of AutoCompleteTextView Dynamically

Set Padding of AutoCompleteTextView

We use android:padding=”” attribute to set padding of AutoCompleteTextView.
We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in AutoCompleteTextView using android:padding=”” attribute.
Learn to Set Padding of AutoCompleteTextView Dynamically

Set Margin of AutoCompleteTextView

We use android:layout_margin=”” attribute to set margin of AutoCompleteTextView.
We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in AutoCompleteTextView using android:layout_margin=”” attribute.
Learn to Set Margin of AutoCompleteTextView Dynamically

Set Background of AutoCompleteTextView

We use android:background=”” attribute to set background of AutoCompleteTextView.
We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in AutoCompleteTextView using android:background=”” attribute.
Learn to Set Background of AutoCompleteTextView Dynamically

Set Visibility of AutoCompleteTextView

We use android:visibility=”” attribute to set visibility of AutoCompleteTextView.
We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of AutoCompleteTextView using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of AutoCompleteTextView Dynamically

Set Text of AutoCompleteTextView

We use android:text=”” attribute to set text of AutoCompleteTextView.
We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        />

Here, we have set text (“Hello Tutorialwing”) in AutoCompleteTextView using android:text=”” attribute.
Similarly, we can set any text using this attribute.
Learn to Set Text of AutoCompleteTextView Dynamically

Set Color of Text in AutoCompleteTextView

We use android:textColor=”” attribute to set color of text in AutoCompleteTextView.
We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textColor="#ffffff"     
        />

Here, we have set color (#ffffff i.e. white) of text (“Hello Tutorialwing”) in AutoCompleteTextView using android:textColor=”” attribute. Similarly, we can set any color using this attribute.
Learn to Set Color of AutoCompleteTextView Dynamically

Set Gravity of AutoCompleteTextView

We use android:gravity=”” attribute to set gravity of text in AutoCompleteTextView.
We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:gravity="center_horizontal"
        />

Here, we have set gravity of text in AutoCompleteTextView using android:gravity=”” attribute. Attribute value can be – “center_horizontal”, “center”, “center_vertical” etc.
Learn to Set Gravity of AutoCompleteTextView Dynamically

Set Text in Uppercase, Lowercase

If we need to show text of AutoCompleteTextView in uppercase or lowercase etc.

Set text in uppercase

We can use android:textAllCaps=”true” attribute to set text in uppercase. We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textAllCaps="true"
        />

Attribute android:textAllCaps=”true” sets text in uppercase. So, HELLO TUTORIALWING is set in AutoCompleteTextView.

By default, false is set in this attribute. So, Whatever value is written in android:text=”” attribute, it will be set as it is. For example,

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textAllCaps="false"
        />

Above code will set Hello Tutorialwing to AutoCompleteTextView.

How do we set text in lowercase?

Answer –

  • In xml file – write all the text in lowercase.
  • In kotlin file – take text as string. Then, convert it in lowercase. Then, set it to AutoCompleteTextView.

Learn to Set Text in Uppercase or Lowercase Dynamically

Set Size of Text in AutoCompleteTextView

We use android:textSize=”” attribute to set size of text in AutoCompleteTextView.
We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textSize="20sp"
        />

Here, we have set size of text in AutoCompleteTextView using android:textSize=”” attribute.
Learn to Set Size of Text of AutoCompleteTextView Dynamically

Set Style (Bold/italic) of Text in AutoCompleteTextView

We use android:textStyle=”” attribute to set style (bold, italic etc.) of text in AutoCompleteTextView.
We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:textStyle="bold"
        />

Here, we have set style of text in AutoCompleteTextView using android:textStyle=”” attribute. This attribute can take bold, italic or normal.
Learn to Set Style of Text of AutoCompleteTextView Dynamically

Set Letter Spacing of Text in AutoCompleteTextView

We use android:letterSpacing=”” attribute to set spacing between letters of text in AutoCompleteTextView.
We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:letterSpacing="1"
        />

Here, we have set spacing between letters of text in AutoCompleteTextView using android:letterSpacing=”” attribute.
Learn to Set Letter Spacing of Text of AutoCompleteTextView Dynamically

Set Typeface of Text in AutoCompleteTextView

We use android:typeface=”” attribute to set typeface in AutoCompleteTextView.
We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:typeface="sans"
        />

Here, we have set typeface of text in AutoCompleteTextView using android:typeface=”” attribute. This attribute can take values – “sans”, “normal”, “monospace” or “normal”.
Learn to Set Typeface of AutoCompleteTextView Dynamically

Set fontFamily of Text in AutoCompleteTextView

We use android:fontFamily=”” attribute to set fontFamily of text in AutoCompleteTextView.
We can do it as below –

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Tutorialwing"
        android:fontFamily="sans-serif"
        />

Here, we have set fontFamily (Here, sans-serif) of text in AutoCompleteTextView using android:fontFamily=”sans-serif” attribute.

Till now, we have see how to use android AutoCompleteTextView using Kotlin. We have also gone through different attributes of AutoCompleteTextView to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Different Attributes of Android AutoCompleteTextView Widget

Below are the various attributes that are used to customise android AutoCompleteTextView Widget. However, you can check the complete list of attributes of AutoCompleteTextView in it’s official documentation site. Here, we are going to list some of the important attributes of this widget –

Some of the popular attributes of AutoCompleteTextView are –

Sr. XML Attributes Description
1 android:completionHint Specifies hint to show in the dropdown menu
2 android:completionHintView Specifies hint view to show in the dropdown menu
3 android:completionThreshold Specifies number of character that user must type before suggestion is displayed in drop down menu
4 android:dropDownAnchor View to anchor the auto-complete dropdown to
5 android:dropDownHeight Sets basic height of the dropdown
6 android:dropDownHorizontalOffset Sets amount of pixel by which dropdown is to be offset horizontally
7 android:dropDownSelector It specifies selector in dropdown list
8 android:dropDownWidth Defines basic width of the dropdown

Some of the popular attributes of android AutoCompleteTextview inherited from TextView are –

Sr. XML Attributes Description
1 android:height Defines height of the view
2 android:width Defines width of the view
3 android:textStyle Defines style of the text. For example, bold, italic or bolditalic etc.
4 android:textSize Defines size of the text

Some of the popular attributes of android autoCompleteTextview inherited from View are –

Sr. XML Attributes Description
1 android:alpha Defines alpha of the view
2 android:background Defines Background of the view
3 android:clickable Defines whether view is clickable or not
4 android:focusable Defines whether view can take focus or not

We have seen different attributes of AutoCompleteTextView and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is AutoCompleteTextView, how can we use android AutoCompleteTextView using Kotlin ? etc. We also went through different attributes of android AutoCompleteTextView.