---
title: "AutoComplete TextBox in Android"  
description: "In this article I am going to explain how to create an AutoComplete textbox by using AutoCompleteTextView widget in an Android application. In this ar"  
author: "Chris Anderson"  
published: 2011-10-21  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/788/autocomplete-textbox-in-android  
category: "android"  
tags: ["android"]  
reading_time: 3 minutes  

---

# AutoComplete TextBox in Android

In this article I am going to explain how to create an [AutoComplete textbox](https://www.mindstick.com/blog/625/autocomplete-textbox-using-bootstrap-in-asp-dot-net) by using **AutoCompleteTextView** widget in an [Android application](https://www.mindstick.com/articles/1653/creating-libraries-for-android-applications). In this article I am going to create AutoCompleteTextView widget that provides suggestions for student names.\

· Start a new project named AutoCompleteDemo.

· Create a [XML file](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp) named **list_item.xml and** insert the following code in the xml.

```
<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"

                android:layout_width="fill_parent"

                android:layout_height="fill_parent"

                android:padding="10dp"

                android:textSize="16sp"

                android:textColor="#000">

</TextView>
```

This file simple define a **TextView** that will be used for each item that appears in the list of suggestions.

· Open the **res/layout/main.xml** and add an AutoCompleteTextView widget inside the linear layout.

```
<?xml  version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
        <TextView android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:text="Student Names"/>
        <AutoCompleteTextView  android:id="@+id/autocompletetextview"
                               android:layout_width="fill_parent"
                               android:layout_height="wrap_content"
                               android:layout_marginLeft="5dp" />
</LinearLayout>
```

Open **res/values/strings.xml** and the following [resources](https://www.mindstick.com/interview/1265/what-are-the-different-types-of-resources-through-which-cold-fusion-can-communicate):

```
<?xml  version="1.0" encoding="utf-8"?>
<resources>
     <string name="hello">Hello World, AutoCompleteActivity!</string>
     <string name="app_name">AutoCompleteDemo</string>
     <string-array name="studentname_array">
         <item>Rohit</item>
         <item>Rahul</item>
         <item>Amit</item>
         <item>Arun</item>
         <item>Rati</item>
         <item>Aman</item>
         <item>Farhan</item>
         <item>Farhad</item>
         <item>Ritika</item>
     </string-array>
</resources>
```

· Open the [Activity](https://www.mindstick.com/forum/23119/how-to-save-activity-state-in-android) (java) file and apply the following code:

```
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class AutoCompleteActivity  extends Activity {

     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);

         AutoCompleteTextView txtview =(AutoCompleteTextView)
                                             findViewById(R.id.autocompletetextview);
         String [] names=getResources().getStringArray(R.array.studentname_array);
         ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
                                                            R.layout.
                                                           R.layout.list_item,names);
         txtview.setAdapter(adapter);
    }
}
```

After the content view is set to the main.xml layout, the AutoCompleteTextView() widget is captured from the layout with **findViewById(int)**. Then we get all the student names in an array from the [resource file](https://www.mindstick.com/articles/327263/what-is-the-use-of-resource-file-how-to-use-it-on-your-project). A new array adapter is then initialized to bind the **list_item.xml** layout to each list in the names [string array](https://www.mindstick.com/forum/12803/check-for-any-item-in-string-array). Then **setAdpater()** is called to associate the ArrayAdapter with the **AutoCompleteTextView** widget so that the string array will [populate](https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box) the list of suggestions.

· Run the application.\

##### The output look something like below:

![AutoComplete TextBox in Android](https://www.mindstick.com/mindstickarticle/68e2fe7d-1673-4475-b971-49694dd89fa3/images/b40090df-fa65-40c7-8f23-68d809e130f2.png)

When the users press some keyword, it will display the related student names automatically from the resource file.

\

---

Original Source: https://www.mindstick.com/articles/788/autocomplete-textbox-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
