Search code examples
androidapiemailgmailemail-address

How can i get suggestion for Email addresses while typing the "To" filed?


I am writing an app that will send an email using the Java Mail external library. I searched for a week about how can i display to user Email addresses suggestions, like the ones being displayed in Gmail.

Any help is appritiated!


Solution

  • Ok, after a bit digging, here is what i found. Take the answer about auto complete TextView, add to it the following and you get what i was looking for.

    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
        while (cursor.moveToNext()) { 
            String contactId = cursor.getString(cursor.getColumnIndex( 
                    ContactsContract.Contacts._ID)); 
            String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
            if (Boolean.parseBoolean(hasPhone)) { 
                // You know it has a number so now query it like this
                Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
                while (phones.moveToNext()) { 
                    String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));                 
                } 
                phones.close(); 
            }
    
            Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); 
            while (emails.moveToNext()) { 
                // This would allow you get several email addresses 
                String emailAddress = emails.getString( 
                        emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
            } 
            emails.close(); 
        }
        cursor.close();