Praveen Praveen - 1 month ago 17
Android Question

Get the Email from Contacts using AutoComplete TextView?

In my application i've one

AutoComplete TextView
and
EditText
In this
AutoComplete TextView
provides all the contact names from Contacts.

I want to get the primary email id which contact was selected in my AutoComplete Textview to the editText. How can i achieve this? Anyone guide me?

Answer

Make sure you have READ_CONTACTS permission for your App.

    AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
    Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);
    startManagingCursor(emailCursor);
    autoCompleteTextView.setAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, emailCursor, new String[] {Email.DATA1}, new int[] {android.R.id.text1}));
    autoCompleteTextView.setThreshold(0);

Please Note AutoCompleteTextView is Case Sensitive.