android how an EditText work as AutoComplete

Viewed 108093

I want my EditText should work as AutoComplete, for that I write in XML file

android:inputType="textAutoComplete|textAutoCorrect"

but it's not working.

I am working with API v2.2 and my Activity extends MapActivity, there I put a simple EditText and a button named "Search". so if we type the location name in EditText and press search button means it should go to that location in map. So I want that EditText to work as a AutoComplete. How can I do that?

5 Answers

I use this code:

enter image description here

1) On AndroidManifest.xml

<uses-permission android:name="android.permission.GET_ACCOUNTS"></uses-permission>

2) On xml layout you must use AutoCompleteTextView instead of EditText.

<AutoCompleteTextView
    android:id="@+id/autoCompleteTextView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:text="AutoCompleteTextView" />

3) Use this on Activity file

private ArrayAdapter<String> getEmailAddressAdapter(Context context) {
    Account[] accounts = AccountManager.get(context).getAccounts();
    String[] addresses = new String[accounts.length];
    for (int i = 0; i < accounts.length; i++) { 
        addresses[i] = accounts[i].name;
    }
    return new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, addresses);
}

4) On onCreate activity:

AutoCompleteTextView autoCompleteTextView1 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView1);
autoCompleteTextView1.setAdapter(getEmailAddressAdapter(this));
Related