AutoCompleteTextView Example


An AutoCompleteTextView is a View that is similar to EditText, except that it shows a list of completion suggestions automatically while the user is typing.

Here we will create a simple app which displays an AutoCompleteTextView in which we can type the name of any international airport in India. The app will display the list of airports matching the user input.

Step 1: Create a new project with name AutoCompleteTextView and package name com.mysample.autocompletetext. Fill the forms and click “Finish” button.

Step 2: Open res -> layout -> xml (or) main.xml and add codes for displaying a TextView textview1, and an AutoCompleteTextView autoCompleteTextView1.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	xmlns:tools="http://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	tools:context=".MainActivity">

	<TextView
		android:id="@+id/textView1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignParentTop="true"
		android:layout_centerHorizontal="true"
		android:layout_marginTop="25dp"
		android:text="Destination Airport:"
		android:textSize="18sp"
		android:textStyle="bold"/>

	<AutoCompleteTextView
		android:id="@+id/autoCompleteTextView1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_alignLeft="@+id/textView1"
		android:layout_below="@+id/textView1"
		android:layout_marginTop="10dp"
		android:ems="10"/>

</RelativeLayout>

Step 3: Open app -> java -> package and open MainActivity.java. Add following code in it.

package com.mysample.autocompletetext;

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

public class MainActivity extends Activity {
	AutoCompleteTextView autocomplete;
	
	// List of all autocomplete options
	String[] airports = { "Indira Gandhi International Airport, Delhi",
		"Chhatrapati Shivaji International Airport, Mumbai",
		"Kempegowda International Airport, Bengaluru", 
		"Chennai International Airport, Chennai",
		"Netaji Subhas Chandra Bose International Airport, Kolkata",
		"Rajiv Gandhi International Airport, Hyderabad",
		"Cochin International Airport, Kochi",
		"Sardar Vallabhbhai Patel International Airport, Ahmedabad",
		"Goa International Airport, Goa",
		"Pune International Airport, Pune",
		"Chaudhary Charan Singh International Airport, Lucknow",
		"Thiruvananthapuram International Airport, Thiruvananthapuram",
		"Coimbatore International Airport, Coimbatore",
		"Calicut International Airport, Calicut",
		"Biju Patnaik International Airport, Bhubaneswar",
		"Dr. Babasaheb Ambedkar International Airport, Nagpur",
		"Sheikhul Aalam International Airport, Srinagar",
		"Jay Prakash Narayan International Airport, Patna",
		"Mangaluru Airport, Mangaluru",
		"Chandigarh International Airport, Chandigarh",
		"Lal Bahadur Shastri International Airport, Varanasi",
		"Veer Savarkar International Airport, Port Blair",
		"Sri Guru Ram Dass Jee International Airport, Amritsar",
		"Tiruchirappalli International Airport, Tiruchirappalli",
		"Lokpriya Gopinath Bordoloi International Airport, Guwahati",
		"Visakhapatnam International Airport"};

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

		autocomplete = findViewById(R.id.autoCompleteTextView1);
		
		// An adapter for displaying a String list
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, airports);
		// Show auto complete options after 2 or more letters are entered by the user
		autocomplete.setThreshold(2);
		// Set adapter for AutoCompleteTextView
		autocomplete.setAdapter(adapter);
	}
}

Output:
Now run the app. In the AutoCompleteTextView field, when you enter two or more letters, you will see the options matching the entry. If you select any option, it will be set as the text of AutoCompleteTextView.

Leave a Reply