Android AutoCompleteTextView Example

0

In this article, I will show you an example of Android’s AutoCompleteTextView widget. It is a subclass of EditText view with the ability to provide suggestions to users as they type. The suggestions are fed in the form of an Adapter. In this example, we will use an ArrayAdapter. We also set an item listener which will simply print the item user has selected.

Main Screen

Our main screen contains an auto-complete widget <AutoCompleteTextView>. We want the user to enter a month name here so have used an auto-complete widget. We will provide the months in an array form in strings.xml.

welcome.xml:

<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.javarticles.android.DatePickerExample" >

    <TextView
        android:id="@+id/welcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="18dp"
        android:text="@string/welcome"
        android:textColor="@color/welcome_text_color"
        android:textSize="20sp" />

    <AutoCompleteTextView
        android:id="@+id/months"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/auto_complete_hint">

        <requestFocus />
    </AutoCompleteTextView>

</LinearLayout>

strings.xml:

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

    <string name="app_name">JavArticles</string>
    <string name="welcome">Auto-Complete Example</string>
    <string name="auto_complete_hint">Enter text</string>
    
    <string-array name="months">
        <item>January</item>
        <item>February</item>
        <item>March</item>
        <item>April</item>
        <item>May</item>
        <item>June</item>
        <item>July</item>
        <item>August</item>
        <item>September</item>
        <item>October</item>
        <item>November</item>
        <item>December</item>
    </string-array>
    
</resources>

Main Activity

In the main activity, we get the array of months and then set the adapter.

final String[] months = getResources().getStringArray(R.array.months);
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, months);

Threshold is set to 1 so that as soon user types in the first character, we can show the suggestions.

autoCompleteTextView.setThreshold(1);

We next set OnItemClickListener listener using setOnItemClickListener. Here we will show the item selected. Item is fetched using:

parent.getItemAtPosition(position)

MainActivity:

package com.javarticles.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private AutoCompleteTextView autoCompleteTextView;

	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);

		autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.months);
		final String[] months = getResources().getStringArray(R.array.months);
		ArrayAdapter arrayAdapter = new ArrayAdapter(this,
				android.R.layout.simple_list_item_1, months);
		autoCompleteTextView.setAdapter(arrayAdapter);
		autoCompleteTextView.setThreshold(1);

		autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(
						parent.getContext(),
						"Selected Value: " + parent.getItemAtPosition(position),
						Toast.LENGTH_LONG).show();
			}
		});
	}
}

Run the application

Type-in the first letter of the month.

auto_complete_main

Auto-Complete Widget

Select June Month.

Select Item from Suggested List

Select Item from Suggested List

Download the source code

This was an example of Android’s AutoCompleteTextView widget. You can download the source code here: autoCompleteTextViewExample.zip

Share.

Comments are closed.