Browse By

Android AutoCompleteTextView Example

Create AutoCompleteTextView Example in Android Studio 2.0.

AutocompleteTextView is 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.

The list of suggestions is obtained from a data adapter and appears only after a given number of characters defined by the threshold.

Direct Download Full Source Code

 

Android AutoCompleteTextView Example

Let’s create an Android Application which shows use of AutoCompleteTextView. It is Simple application which allows you to select your country.

Android Auto complete text view example

Auto Complete Text View Demo

Edit man.xml file

<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"
    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=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pulse 7 Demo for Auto Complete Text View"
        android:textColor="#00f"
        android:textSize="18sp" />

    <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="36dp"
        android:completionThreshold="1"
        android:ems="10"
        android:hint="Select Country" />

</RelativeLayout>

Now Edit your Activity File like this way

package com.vrs.pulse7demo;

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 autoCompleteTextView;

	String countries[] = { "INDIA", "ITALY", "JAPAN", "USA", "ICELAND",
			"INDONESIA", "UK", "IRAN", "IRAQ" };

	ArrayAdapter<String> adapter;

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

		autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_dropdown_item_1line, countries);

		autoCompleteTextView.setAdapter(adapter);

	}

}

Point to Remember

android:completionThreshold=”1″ properties of AutoCompleteTextView specifies the minimum number of characters the user has to type in the edit box before the drop down list is shown. When completionThreshold is less than or equals 0, a threshold of 1 is applied.

I hope you like this article. Share your views to to improve content.

Subscribe for more articles!!

Download Full Source Code

 

 

 

  • $@nU

    Nice work ever Thx sirji…

    • pulse7

      My pleasure, stay connected for new tutorials.