Friday, March 23, 2012

AutoComplete textView in android.


AutoComplete textView in android.

Here is a simple example to create an autocomplete textView in android.
After creating an array of values for the dropdown we will set it as adapter for the textView.
Check out the code.
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
package pack.coderzheaven;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
 
public class AutoCompleteDemo extends Activity {
     static final String[] COUNTRIES = new String[] {
            "India","America","Canada", "Indonesia","England","Britian"};
    @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, COUNTRIES);
        AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit);
        textView.setAdapter(adapter);
    }
}
Here is the main.xml file
?
Drag and copy the code
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?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="wrap_content">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please type in a country name." />
 
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
 
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Country : " />
 
        <AutoCompleteTextView android:id="@+id/edit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
 
    </LinearLayout>
</LinearLayout>
AutoCompleteDemo
AutoCompleteDemo

No comments: