1page.title=Hello, AutoCompleteTextView 2parent.title=Hello, Views 3parent.link=index.html 4@jd:body 5 6<p>{@link android.widget.AutoCompleteTextView} is an implementation of the EditText widget that will provide 7auto-complete suggestions as the user types. The suggestions are extracted from a collection of strings.</p> 8 9 10<ol> 11 <li>Start a new project/Activity called HelloAutoComplete.</li> 12 <li>Open the layout file. 13 Make it like so: 14<pre> 15<?xml version="1.0" encoding="utf-8"?> 16<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 17 android:orientation="horizontal" 18 android:layout_width="fill_parent" 19 android:layout_height="wrap_content"> 20 21 <TextView 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:text="Country" /> 25 26 <AutoCompleteTextView android:id="@+id/edit" 27 android:layout_width="fill_parent" 28 android:layout_height="wrap_content"/> 29 30</LinearLayout> 31</pre> 32</li> 33 34<li>Open HelloAutoComplete.java and insert the following as the <code>onCreate</code> method: 35<pre> 36@Override 37protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.main); 40 41 AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit); 42 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 43 android.R.layout.simple_dropdown_item_1line, COUNTRIES); 44 textView.setAdapter(adapter); 45} 46</pre> 47 <p>Here, we create an AutoCompleteTextView from our layout. We then 48 create an {@link android.widget.ArrayAdapter} that binds a <code>simple_dropdown_item_1line</code> 49 layout item to each entry in the <code>COUNTRIES</code> array (which we'll add next). 50 The last part sets the ArrayAdapter to associate with our AutoCompleteTextView.</p> 51</li> 52 53<li>After the <code>onCreate()</code> method, add the String array: 54<pre> 55static final String[] COUNTRIES = new String[] { 56 "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", 57 "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", 58 "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", 59 "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", 60 "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", 61 "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", 62 "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", 63 "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde", 64 "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", 65 "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", 66 "Cook Islands", "Costa Rica", "Croatia", "Cuba", "Cyprus", "Czech Republic", 67 "Democratic Republic of the Congo", "Denmark", "Djibouti", "Dominica", "Dominican Republic", 68 "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", 69 "Estonia", "Ethiopia", "Faeroe Islands", "Falkland Islands", "Fiji", "Finland", 70 "Former Yugoslav Republic of Macedonia", "France", "French Guiana", "French Polynesia", 71 "French Southern Territories", "Gabon", "Georgia", "Germany", "Ghana", "Gibraltar", 72 "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", 73 "Guyana", "Haiti", "Heard Island and McDonald Islands", "Honduras", "Hong Kong", "Hungary", 74 "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", 75 "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Kuwait", "Kyrgyzstan", "Laos", 76 "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", 77 "Macau", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", 78 "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia", "Moldova", 79 "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", 80 "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", 81 "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "North Korea", "Northern Marianas", 82 "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", 83 "Philippines", "Pitcairn Islands", "Poland", "Portugal", "Puerto Rico", "Qatar", 84 "Reunion", "Romania", "Russia", "Rwanda", "Sqo Tome and Principe", "Saint Helena", 85 "Saint Kitts and Nevis", "Saint Lucia", "Saint Pierre and Miquelon", 86 "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Saudi Arabia", "Senegal", 87 "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", 88 "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "South Korea", 89 "Spain", "Sri Lanka", "Sudan", "Suriname", "Svalbard and Jan Mayen", "Swaziland", "Sweden", 90 "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "The Bahamas", 91 "The Gambia", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", 92 "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Virgin Islands", "Uganda", 93 "Ukraine", "United Arab Emirates", "United Kingdom", 94 "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", 95 "Vanuatu", "Vatican City", "Venezuela", "Vietnam", "Wallis and Futuna", "Western Sahara", 96 "Yemen", "Yugoslavia", "Zambia", "Zimbabwe" 97}; 98</pre> 99 <p>This is the list of suggestions that will be offered as the user types into the 100 AutoCompleteTextView.</p> 101</li> 102 103<li>Now run it.</li> 104</ol> 105<p>As you type, you should see something like this:</p> 106<img src="images/hello-autocomplete.png" width="150px" /> 107 108 109<h3>References</h3> 110<ul> 111 <li>{@link android.R.layout}</li> 112 <li>{@link android.widget.ArrayAdapter}</li> 113 <li>{@link android.widget.AutoCompleteTextView}</li> 114</ul> 115 116 117