Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
AutoComplete
2.1.
Attributes of AutoCompleteTextView
2.2.
Example Application
3.
Frequently Asked Questions
4.
Conclusion  
Last Updated: Mar 27, 2024
Easy

Android AutoComplete Feature

Author Pradeep Kumar
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

  1. What is the function of setThreshold in an AutoCompleteTextView?
    It specifies the number of characters that the user has to type before the suggestion drop-down menu pops down.
  2. What is the use of the adapter class in an AutoCompleteTextView?
    It is responsible for showing the list of suggestions in the drop-down menu.
  3. What is the maximum number of suggestions that should be presented to users?
    The sweet spot for speeding up a user's search is displaying ten choices. When more than ten autocomplete options are displayed, searchers become overwhelmed and have difficulty choosing the right suggestion.

Conclusion  

In this article, we have extensively discussed the AutoComplete Feature in Android and its implementation in Android Studio. We discussed how AutoComplete Feature could be created and registered in an android application.

We hope that this blog has helped you enhance your knowledge regarding the AutoComplete Feature. All the widgets are enclosed inside a UI layout and if you would like to learn more about layouts, check out our article on Android UI layouts. And to learn in-depth about android development, check out our Android Development course on the Coding Ninjas website. Do upvote our blog to help other ninjas grow. Happy Coding!

Previous article
Audio Manager in Android
Next article
Android Relative Layout
Live masterclass