In this article I am going to explain how to create an AutoComplete textbox by using AutoCompleteTextView widget in an Android application. 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 named list_item.xml and insert the following code in the xml.

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

<TextView xmlns:android=""







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=""
        <TextView android:layout_width="fill_parent"
                  android:text="Student Names"/>
        <AutoCompleteTextView android:id="@+id/autocompletetextview"
                               android:layout_marginLeft="5dp" />

Open res/values/strings.xml and the following resources:

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

     <string name="hello">Hello World, AutoCompleteActivity!</string>
     <string name="app_name">AutoCompleteDemo</string>
     <string-array name="studentname_array">

·         Open the Activity (java) file and apply the following code:


import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class AutoCompleteActivity extends Activity {

     public void onCreate(Bundle savedInstanceState) {

         AutoCompleteTextView txtview =(AutoCompleteTextView)
         String [] names=getResources().getStringArray(R.array.studentname_array);
         ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,

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. A new array adapter is then initialized to bind the list_item.xml layout to each list in the names string array. Then setAdpater() is called to associate the ArrayAdapter with the AutoCompleteTextView widget so that the string array will populate the list of suggestions.

·         Run the application.

The output look something like below:

AutoComplete TextBox in Android

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

  Modified On Mar-23-2018 02:08:20 AM

Leave Comment