Add the initial structure for your AutoComplete class to

public class AutoComplete extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle);

This class gives you a base to start building the rest of your Activity. All the functionality of this Activity will be built around this class. The first thing you need to do is load the layout from autocomplete.xml:


For this example, you will create the AutoCompleteTextView so that it contains a list of the months of the year. When a user types into the box, it will anticipate which month they are trying to enter. Given that the AutoCompleteTextView will contain a list of the months, you need to create a list that can be assigned to the AutoCompleteTextView. Create a string array and assign the month values to it:

static final String[] Months = new String[]{


"September","October","November","December" };

The next task is to assign this string array to the AutoCompleteTextView. You have created more than a few Views by now, so the code to create the AutoCompleteTextView should look very familiar. What you have not seen before is the code to assign the string array to the View:

ArrayAdapter<String> monthArray = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Months); final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(; textView.setAdapter(monthArray);

In the first line, you are taking the string array you created and assigning it to an ArrayAdapter named monthArray. Next, you instantiate your AutoCompleteTextView by locating it in the .xml layout file. Finally, you use the setAdapter( ) method to assign the monthArray ArrayAdapter to the AutoCompleteTextView.

The next snippet of code instantiates the two Buttons. This is the same code you have used in previous chapters. The only difference here from other code you have written is that you are calling two functions, changeOption and changeOption2, which have not been created yet.

Was this article helpful?

0 0

Post a comment