Skip to content

Instantly share code, notes, and snippets.

@h4ck4life
Created October 11, 2013 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save h4ck4life/6941661 to your computer and use it in GitHub Desktop.
Save h4ck4life/6941661 to your computer and use it in GitHub Desktop.
Android AutoCompleteTextView example code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/search_item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#000"
android:textSize="16sp" >
</TextView>
</LinearLayout>
final String[] PRODUCTS = new String[] { "Afghanistan", " Albania", " Algeria", " American Samoa", " Andorra", " Angola", " Anguilla", " Antigua and Barbuda", " Argentina", " Armenia", " Aruba", " Australia", " Austria", " Azerbaijan", " Bahamas", " The", " Bahrain", " Bangladesh", " Barbados", " Belarus", " Belgium", " Belize", " Benin", " Bermuda", " Bhutan", " Bolivia", " Bosnia", " Botswana", " Bougainville", " Brazil", " British Indian Ocean", " British Virgin Islands", " Brunei", " Bulgaria", " Burkina Faso", " Burundi", " Cambodia", " Cameroon", " Canada", " Cape Verde Islands", " Cayman Islands", " Central African Republic", " Chad", " Chile", " China", " Hong Kong", " China", " Macau", " China", " People’s Republic", " China", " Taiwan", " Colombia", " Comoros", " Congo", " Democratic Republic of", " Congo", " Republic of", " Cook Islands", " Costa Rica", " Cote d’Ivoire", " Croatia", " Cuba", " Cyprus", " Czech Republic", " Denmark", " Djibouti", " Dominica", " Dominican Republic", " Ecuador", " Egypt", " El Salvador", " Equatorial Guinea", " Eritrea", " Estonia", " Ethiopia", " Faeroe Islands", " Falkland Islands", " Federated States of Micronesia", " Fiji", " Finland", " France", " French Guiana", " French Polynesia", " Gabon", " Gambia", " The", " Georgia", " Germany", " Ghana", " Gibraltar", " Greece", " Greenland", " Grenada", " Guadeloupe", " Guam", " Guatemala", " Guinea", " Guinea-Bissau", " Guyana", " Haiti", " Holy See (Vatican City State)", " Honduras", " Hungary", " Iceland", " India", " Indonesia", " Iran", " Iraq", " Ireland", " Israel", " Italy", " Jamaica", " Japan", " Jordan", " Kazakhstan", " Kenya", " Kiribati", " Korea", " Democratic People’s Rep", " Korea", " Republic of", " Kosovo", " Kuwait", " Kyrgyzstan", " Laos", " Latvia", " Lebanon", " Lesotho", " Liberia", " Libya", " Liechtenstein", " Lithuania", " Luxembourg", " Macedonia", " Madagascar", " Malawi", " Malaysia", " Maldives", " Mali", " Malta", " Martinique", " Mauritania", " Mauritius", " Mayotte", " Mexico", " Moldova", " Monaco", " Mongolia", " Montenegro", " Montserrat", " Morocco Mozambique", " Myanmar", " Namibia", " Nauru", " Nepal", " Netherlands", " Netherlands Antilles", " New Caledonia", " New Zealand", " Nicaragua", " Niger", " Nigeria", " Norway", " Oman", " Pakistan", " Palestine", " Panama", " Papua New Guinea", " Paraguay", " Peru", " Philippines", " Poland", " Portugal", " Puerto Rico", " Qatar", " Réunion", " Romania", " Russia", " Rwanda", " Saint Barthelemy", " Saint Helena", " Saint Kitts & Nevis", " Saint Lucia", " Saint Martin", " Saint Pierre & Miquelon", " Saint Vincent", " Samoa", " San Marino", " Sao Tomé & Principe", " Saudi Arabia", " Senegal", " Serbia", " Seychelles", " Sierra Leone", " Singapore", " Slovakia", " Slovenia", " Solomon Islands", " Somalia", " South Africa", " Spain", " Sri Lanka", " Sudan", " Suriname", " Swaziland", " Sweden", " Switzerland", " Syria", " Tajikistan", " Tanzania", " Thailand", " Timor Leste", " Togo", " Tokelau Islands", " Tonga", " Trinidad & Tobago", " Tunisia", " Turkey", " Turkmenistan", " Turks & Caicos Islands", " Tuvalu", " Uganda", " Ukraine", " United Arab Emirates", " United Kingdom of GB & NI", " United States of America", " Uruguay", " US Virgin Islands", " Uzbekistan", " Vanuatu", " Venezuela", " Vietnam", " Wallis & Futuna Islands", " Yemen", " Zambia", " Zimbabwe" };
final AutoCompleteTextView textView = (AutoCompleteTextView) rootView
.findViewById(R.id.autocompletesearch);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
getActivity(),
R.layout.list_item_search_autocomplete,
R.id.search_item,
PRODUCTS);
textView.setAdapter(adapter);
textView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> view, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getActivity().getApplicationContext(), textView.getText(), Toast.LENGTH_SHORT).show();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment