AutoComplete textView in android.

By | August 9, 2011

Here is a simple example to create an autocomplete textView in android.
After creating an array of values for the dropdown we will set it as adapter for the textView.
Check out the code.

package pack.coderzheaven;

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

public class AutoCompleteDemo extends Activity {
	 static final String[] COUNTRIES = new String[] {
			"India","America","Canada", "Indonesia","England","Britian"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, COUNTRIES);
        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit);
        textView.setAdapter(adapter);
    }
}

Here is the main.xml file

<?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="wrap_content">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please type in a country name." />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Country : " />

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

    </LinearLayout>
</LinearLayout>
AutoCompleteDemo

AutoCompleteDemo

Leave a Reply

Your email address will not be published. Required fields are marked *