articles

Home / DeveloperSection / Articles / AutoComplete TextBox in Android

AutoComplete TextBox in Android

Chris Anderson 10051 21-Oct-2011

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="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:

<?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 (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. 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.



Updated 07-Sep-2019
hi I am software developer at mindstick software pvt. ltd.

Leave Comment

Comments

Liked By