Saturday 23 June 2012

AutoCompleteTextView - Example


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.

See the Example below:
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Enter the Name" />    
     <AutoCompleteTextView
    android:id="@+id/act1"
    android:layout_width="180dip"
    android:layout_height="wrap_content"
    android:padding="6dip"
   / > 
</LinearLayout>

Code:
package com.auto.ext;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class Autotext extends Activity {
AutoCompleteTextView auto;
String president[]={"manmohan","Bush","clinton","arafat","vajpayee","brown","apple1","apple2","apple3","apple4"};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        auto=(AutoCompleteTextView)findViewById(R.id.act1);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,president);
        auto.setAdapter(adapter);
        auto.setThreshold(2);

    }
}

See the Output Screen:

More About AutoCompleteTextView Click here..