I will show how to create AutoCompleteTextView and MultiAutoCompleteTextView in android programmatically.
Please try it your self.
package com.j4android;
private AutoCompleteTextView autoComplete;
private MultiAutoCompleteTextView multiAutoComplete;
private ArrayAdapter<String> adapter;
String[] srt_array = { "arjun", "anbu", "anandh", "babu", "balu", "balaji",
"cinna", "dinesh", "darvin", "edward", "edvin", "faishal", "guru",
"ganesh", "harish", "hariharan", "sasi", "varun", "yamini" };
@Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);
// get the defined string-array
String[] colors = getResources().getStringArray(R.array.colorList);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, srt_array);
autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
multiAutoComplete = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoComplete);
// set adapter for the auto complete fields
autoComplete.setAdapter(adapter);
multiAutoComplete.setAdapter(adapter);
// specify the minimum type of characters before drop-down list is shown
autoComplete.setThreshold(1);
multiAutoComplete.setThreshold(2);
// comma to separate the different colors
multiAutoComplete
.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
// when the user clicks an item of the drop-down list
multiAutoComplete.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(),
"" + arg0.getItemAtPosition(arg2), Toast.LENGTH_LONG)
.show();
}
});
}
}
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text1"
android:layout_marginTop="20dp"
android:ems="8" />
<MultiAutoCompleteTextView
android:id="@+id/multiAutoComplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text2"
android:layout_marginTop="20dp"
android:ems="13" />
</LinearLayout>
Please try it your self.
package com.j4android;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;
public class MainActivity extends Activity{import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.MultiAutoCompleteTextView;
import android.widget.Toast;
private AutoCompleteTextView autoComplete;
private MultiAutoCompleteTextView multiAutoComplete;
private ArrayAdapter<String> adapter;
String[] srt_array = { "arjun", "anbu", "anandh", "babu", "balu", "balaji",
"cinna", "dinesh", "darvin", "edward", "edvin", "faishal", "guru",
"ganesh", "harish", "hariharan", "sasi", "varun", "yamini" };
@Override
public void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(R.layout.main);
// get the defined string-array
String[] colors = getResources().getStringArray(R.array.colorList);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, srt_array);
autoComplete = (AutoCompleteTextView) findViewById(R.id.autoComplete);
multiAutoComplete = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoComplete);
// set adapter for the auto complete fields
autoComplete.setAdapter(adapter);
multiAutoComplete.setAdapter(adapter);
// specify the minimum type of characters before drop-down list is shown
autoComplete.setThreshold(1);
multiAutoComplete.setThreshold(2);
// comma to separate the different colors
multiAutoComplete
.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
// when the user clicks an item of the drop-down list
multiAutoComplete.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getBaseContext(),
"" + arg0.getItemAtPosition(arg2), Toast.LENGTH_LONG)
.show();
}
});
}
}
main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text1"
android:layout_marginTop="20dp"
android:ems="8" />
<MultiAutoCompleteTextView
android:id="@+id/multiAutoComplete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text2"
android:layout_marginTop="20dp"
android:ems="13" />
</LinearLayout>