Question

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!

Was it helpful?

Solution 4

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();

OTHER TIPS

Get a list of the emails and use a autocompleteTextView to display them as you type.

Set an adapter to the textBox:

ArrayAdapter<String> namesAdapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_dropdown_item_1line, names);
searchBox.setAdapter(namesAdapter);

searchBox.setOnItemClickListener(new OnItemClickListener() {

       public void onItemClick(AdapterView<?> arg0, View view, int position, long arg3) {
            String email = searchBox.getText().toString(); //or use the position to reference the email address you want.

        });
}

As far as I know, Gmail uses the Gmail AddressBook for the suggestions. For your suggestions you can use the Contacts of your phone, which in case you have synched them with your Gmail AddressBook, they are also available at the Contacts.

Hope this helps! In case you need anything more specific, please do not hesitate to ask.

"Smartr Contacts" http://smartrcontacts.com/ is a good app for Gmail and Android and iPhone that will Synch all ur contacts n merge their email tel mob etc then u can perhaps call what email u want from the dbase.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top