AutoCompleteTextView Example
|
|
AutoCompleteTextView is similar to EditText except that it shows a list of 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.
AutoCompleteTextView Example
Project Name : AutoCompleteTextViewDemo
Package : com.sunil.actvdemo
Build Target : Android 2.2
main.xml
(File : res/layout/main.xml)
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="What is your fav development platform ?"
/>
<AutoCompleteTextView android:id="@+id/actvDev"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="What is your fav development platform ?"
/>
<AutoCompleteTextView android:id="@+id/actvDev"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
AutoCompleteTextViewDemoActivity
(File : AutoCompleteTextViewDemoActivity.java)
package com.sunil.actvdemo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class AutoCompleteTextViewDemoActivity extends Activity {
/** Called when the activity is first created. */
//Creating an create an array of Dev Platforms
String[] devplatforms =
{
"C",
"C++",
"Java",
"C#.NET",
"iPhone",
"Android",
"ASP.NET",
"PHP",
"Python",
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,devplatforms);
AutoCompleteTextView actvDev = (AutoCompleteTextView)findViewById(R.id.actvDev);
actvDev.setThreshold(1);
actvDev.setAdapter(adapter);
}
}
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class AutoCompleteTextViewDemoActivity extends Activity {
/** Called when the activity is first created. */
//Creating an create an array of Dev Platforms
String[] devplatforms =
{
"C",
"C++",
"Java",
"C#.NET",
"iPhone",
"Android",
"ASP.NET",
"PHP",
"Python",
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,devplatforms);
AutoCompleteTextView actvDev = (AutoCompleteTextView)findViewById(R.id.actvDev);
actvDev.setThreshold(1);
actvDev.setAdapter(adapter);
}
}
Demo
When you run this application you see the following output -
And when you type in AutoCompleteTextView it automatically pop-up the suggestions like this -
You can also download the source code of this example.











i really great full to you.you provided such a net and clean way to understand.
i have one more doubt why are u use setThreshold(1) in your programme.let me clarify it.
@Panchanan Rauta
setThreshold() method defines the number of characters that the user must type before completion suggestions are displayed in a drop down menu.
very very helpful example thanx. Your example save my so much time to goggling. Keep posting. Again thanz
Post a Comment