Wednesday 20 June 2012

AutoCompleteTextView in Android

AutoCompleteTextView is a sub class of EditText.

AutoCompleteTextView  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.

The following Eample shows how to create a text view which suggests various states names
while the user is typing:

UI Layout(autocomplite_tv.xml)
<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 your state name ?" />
    <AutoCompleteTextView
        android:id="@+id/states"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Android Activity class(AutoCompliteTextViewExample.java)

package com.androidsurya.autocomplite;

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

public class AutoCompliteTextViewExample extends Activity {
    /** Called when the activity is first created. */

    // Creating an create an array of states in india

    String[] states = { "Andhra Pradesh", "Arunachal Pradesh", "Assam",
            "Bihar", "Chhattisgarh", "Goa", "Gujarat", "Haryana",
            "Himachal Pradesh", "Himachal Pradesh", "Jammu and Kashmir",
            "Jharkhand", "Karnataka", "Kerala", "Madhya Pradesh",
            "Maharashtra", "Manipur", "Meghalaya", "Mizoram", "Nagaland",
            "Odisha", "Punjab", "Rajasthan", "Sikkim", "Tamil Nadu", "Tripura",
            "Uttar Pradesh", "Uttarakhand", "West Bengal" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.autocomplite_tv);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_dropdown_item_1line, states);
        AutoCompleteTextView actvDev = (AutoCompleteTextView) findViewById(R.id.states);
        actvDev.setThreshold(1);
        actvDev.setAdapter(adapter);

    }
}

Register Activity in AndroidManifest file

 <activity
            android:name="com.androidsurya.autocomplite.AutoCompliteTextViewExample"
            android:label="@string/app_name" >

Output Screenshot:


 





For more information about AutoCompliteTextView : Android Developers

No comments:

Post a Comment

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...