Android Autocomplete Textview Example

What is AutoCompleteTextView?

An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.

The drop down can be dismissed at any time by pressing the back key or, if no item is selected in the drop down, by pressing the enter/dpad center key.

Layout architecture:

The application that we are going to develop has a simple screen with TextView.

[pgsubscribe]

Quick Links

Project Structure

Create layout XML

  • Create new android project [File >> New >> Android Project] with project name AutoCompleteTextView
  • Click next and select target android device version [I chose version 2.2]
  • Click next and enter package name – ‘com.prgguru.android’
  • Click finish

Code Listings

Open main.xml, now you can view the layout as either XML or in graphical view and just replace the XML with below one:

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

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

MainActivity.java

  1. Create Array adapter – Define drop down style and string array
  2. Retrieve the AutoCompletTextView widget in UI that we need to interact with programmatically
  3. Set threshold – Number of characters the user must type before the drop down list is shown

  4. Set the array adapter to the AutoCompleteTextView widget object
package com.prgguru.android;

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

public class MainActivity extends Activity {

	String[] languages = { "C","C++","Java","C#","PHP","JavaScript","jQuery","AJAX","JSON" };

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
                //Create Array Adapter
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice, languages);
                //Find TextView control
		AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.languages);
		//Set the number of characters the user must type before the drop down list is shown
                acTextView.setThreshold(1);
                //Set the adapter
		acTextView.setAdapter(adapter);
	}
}

[pglinkadssmall1]

Demo

Let us test the application:
Run click on the project >> Run as >> Android application
You could see following screen:



Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android is the installation file simliar to exe in windows.

[pglinkadssmall]

I hope you enjoyed the post!! ๐Ÿ™‚
Keep posted with your valuable comments below ๐Ÿ™‚

Reference:

Android AutoComplete TextView

[pgfeedback]
[pgwriteforus]

Related Articles

Author: Udhay

Share This Post On
468 ad
  • Hey thanks,
    i have used your source and it works fine but how it show radio button with it in image ?

    • You have to set ‘R.layout.select_dialog_singlechoice’ in ArrayAdapter to show radio button.
      Please see below:
      ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.select_dialog_singlechoice, languages);
      Thanks!

  • abdul

    thanks it works perfect just i want to ask if i there is version in my data with language i want to display bottom of my suggestion list in small text..

Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares