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.
drop down text not visisble only after clicking the drop down list text is visible can u help plz
ReplyDeletewhere is this item: 'simple_dropdown_item_1line' coming from??? don't see it anywhere.....
ReplyDeletehow to change the color of dropdown list box
ReplyDeletedrop down text not visisble only after clicking the drop down list text is visible can u help plz
ReplyDeleteWhat is "ems" field in .xml file ?
ReplyDeleteThe site is looking piece gaudy and it gets the guests eyes. Configuration is really basic and a decent easy to understand interface. LeReviewist
ReplyDeleteThis a good way to appreciate the teacher as they put their efforts to train students. UK dissertation Writers appreciates the teachers. agen asuransi allianz
ReplyDeleteMua vé tại đại lý vé máy bay Aivivu, tham khảo
ReplyDeletegiá vé máy bay đi Mỹ khứ hồi
về việt nam từ mỹ
đặt vé máy bay giá rẻ đi đà nẵng
dat ve may bay ra ha noi
giá vé máy bay đà nẵng nha trang
Hello, Dear ladies! The best Escort agency offer adult erotic sex escort Job as a Escorts Girl Job in Gurgaon. Dear girls, welcome to our agency who provide you the best Female Escorts Job in Gurgaon which exist from many year! We are escort agency, but we work with different areas in India with luxury worldwide models. If you really want and serious about what we offer like: sex love Call Girls job in Gurgaon?
ReplyDeleteThe number of clients and works depends on the appearance of the girl, services, the quality of real photographs and selfies, and of course on a positive attitude as Escorts Girl Job in Gurgaon. We have the highest prices in Delhi escort and the highest percentage for girls. In escort companions in the Delhi, Russian girls have 60% of the income from the transaction, for example for an hour it is 15000-30000 Rs. so Join as a Female Escorts Job in Gurgaon. We are also open for Call Girls job in Gurgaon.
Liên hệ Aivivu, đặt vé máy bay tham khảo
ReplyDeletevé máy bay đi Mỹ giá bao nhiêu
vé máy bay từ mỹ về việt nam hãng korea
vé máy bay từ Toronto về việt nam
vé máy bay từ nhật về việt nam bao nhiêu
mua vé máy bay từ hàn quốc về việt nam
Vé máy bay từ Đài Loan về Việt Nam
giá khách sạn cách ly
Chi phi cho chuyen gia nuoc ngoai
Avantech provides industry-leading geotechnical software for analyzing soil and rock slopes. Designed for civil engineering & mining. Visit us: https://avantech.in/product-category/engineering-software/geotechnical-modeling/
ReplyDeleteexcel Printers Provide the best carry bag printing in Delhi and a Paper Carry bag printing Manufacturer. Though Carry bags in Delhi. Visit us: https://www.excelprinters.com/product/paper-carry-bag-printing-in-delhi/
ReplyDeleteHi,
ReplyDeleteThanks for sharing the information with us it was very informative. https://hangup.in