AutocompleteTextView is 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.
Create A new Project AutocompleteTextView
open your .xml file and write foolowing to get the following layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose The Country Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Choose The Country" >
<requestFocus />
</AutoCompleteTextView>
<TextView
android:id="@+id/textViewCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="You Selected :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/buttonSelectCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_gravity="center_horizontal"
android:text=" OK "
/>
</LinearLayout>
Now Edit your Activitty file
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String COUNTRIES[]={"INDIA","ITALY","JAPAN","USA","ICELAND","INDONESIA","UK","IRAN","IRAQ"};
final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
final TextView textViewSelectedCountry=(TextView)findViewById(R.id.textViewCountry);
Button btnSelectedCountry=(Button)findViewById(R.id.buttonSelectCountry);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
// Set the adapter
autoCompleteTextViewCountry.setAdapter(adapter);
autoCompleteTextViewCountry.setThreshold(1);
btnSelectedCountry.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
String country=autoCompleteTextViewCountry.getText().toString();
textViewSelectedCountry.setText("Selected Country: "+country);
}
});
}
}
Specifies the minimum number of characters the user has to type in the edit box before the drop down list is shown.
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.
Create A new Project AutocompleteTextView
Edit .xml file
open your .xml file and write foolowing to get the following layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_marginTop="40dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Choose The Country Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView1"
android:layout_marginTop="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Choose The Country" >
<requestFocus />
</AutoCompleteTextView>
<TextView
android:id="@+id/textViewCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:text="You Selected :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/buttonSelectCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_gravity="center_horizontal"
android:text=" OK "
/>
</LinearLayout>
Now Edit your Activitty file
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String COUNTRIES[]={"INDIA","ITALY","JAPAN","USA","ICELAND","INDONESIA","UK","IRAN","IRAQ"};
final AutoCompleteTextView autoCompleteTextViewCountry = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
final TextView textViewSelectedCountry=(TextView)findViewById(R.id.textViewCountry);
Button btnSelectedCountry=(Button)findViewById(R.id.buttonSelectCountry);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
// Set the adapter
autoCompleteTextViewCountry.setAdapter(adapter);
autoCompleteTextViewCountry.setThreshold(1);
btnSelectedCountry.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
String country=autoCompleteTextViewCountry.getText().toString();
textViewSelectedCountry.setText("Selected Country: "+country);
}
});
}
}
Point to Remember:
public void setThreshold (int threshold)
Specifies the minimum number of characters the user has to type in the edit box before the drop down list is shown.
When
threshold
is less than or equals 0, a threshold of 1 is applied.
Now run the project
you get following
Hope you like the post . Comments and Questions are welcome.