I want to display only those contact names whose email address is present. Otherwise that contact name should not be displayed in List. How can I do this? Can anybody please help me?
7 Answers
Here is my super fast query to pull email addresses. It is much faster than pulling all contact columns as suggested by other answers...
public ArrayList<String> getNameEmailDetails() {
ArrayList<String> emlRecs = new ArrayList<String>();
HashSet<String> emlRecsHS = new HashSet<String>();
Context context = getActivity();
ContentResolver cr = context.getContentResolver();
String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
String order = "CASE WHEN "
+ ContactsContract.Contacts.DISPLAY_NAME
+ " NOT LIKE '%@%' THEN 1 ELSE 2 END, "
+ ContactsContract.Contacts.DISPLAY_NAME
+ ", "
+ ContactsContract.CommonDataKinds.Email.DATA
+ " COLLATE NOCASE";
String filter = ContactsContract.CommonDataKinds.Email.DATA + " NOT LIKE ''";
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, PROJECTION, filter, null, order);
if (cur.moveToFirst()) {
do {
// names comes in hand sometimes
String name = cur.getString(1);
String emlAddr = cur.getString(3);
// keep unique only
if (emlRecsHS.add(emlAddr.toLowerCase())) {
emlRecs.add(emlAddr);
}
} while (cur.moveToNext());
}
cur.close();
return emlRecs;
}
I tried the code provided by 'Agarwal Shankar' but it took about 4 seconds to get contacts on my test device, and this code took about 0.04 sec.
-
Ultimate Answer.. Must Use this one – Gaurav Arora Jun 13 '13 at 10:00
-
Super fast, way faster then the other solution. – Evgeny Erlihman Jul 06 '13 at 13:28
-
This should be the answer. Not the other one. +1 – alicanbatur Jan 14 '14 at 09:31
-
It is really super fast comparing to non-filter example. Thanks! – lomza Dec 07 '14 at 18:29
-
3What does the sql command in order variable mean? Especially "THEN 1 ELSE 2" is somewhat confusing. – Aziz Murtazaev Jan 27 '15 at 05:32
-
2I test it and it work for most of the device, but on my LG G3 5.0, this not working!!! – douarbou Jul 15 '15 at 21:38
-
@ChandraSekhar Tested on my Lollipop devices and it works. – Skizo-ozᴉʞS ツ Oct 03 '15 at 17:35
-
Is there any way to get the photo of the contact? – Skizo-ozᴉʞS ツ Oct 03 '15 at 17:35
-
1Hi, I wrote same query to get contact names whose sip address is present. But it is not working. I am not able to figure out what content resolver default value for sip address when sip address is not there – Prashanth Debbadwar Nov 05 '15 at 08:20
-
5@douarbou I am not sure if you still need it but I have same issue with LG G3 you can fix it by replacing "NOT LIKE" with "<>" in filter – Varun Narisetty Dec 22 '15 at 16:19
-
This is Really a super fast, You release the headache. – hims_3009 Jan 01 '16 at 14:20
-
Does the query run on main thread or does it use a different thread? Is there a problem if it runs on the main thread? – marios-codes Jan 03 '18 at 18:58
-
Can you explain what does your `order` does in this case? I have previously written a function which has pagination on it and I'm not sure where I can insert it there. – mr5 Feb 12 '18 at 10:34
-
Hi, Along with emails how can i get email type? Can you tell me how can i add it in this code? – Devarsh Ranpara Dec 07 '20 at 09:18
public ArrayList<String> getNameEmailDetails(){
ArrayList<String> names = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
//to get the contact names
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", name);
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email", email);
if(email!=null){
names.add(name);
}
}
cur1.close();
}
}
return names;
}
the above method return an arraylist of names which has email id.

- 34,573
- 7
- 66
- 64
-
I am getting contact names and email address in ArrayList. I saw in log cat like this: Name: abc@gmail.com, Email: abc@gmail.com, Name: xyz@gmai.com, Email: xyz@gmail.com, Name: xyz, Email:xyz@gmail.com. Means I am getting mixture of names and email address in contact name :( – Krishna Suthar Apr 12 '12 at 05:44
-
-
1
-
1
-
This method works but it is slow, prefer the answer of Marcin. – Aurélien Guillard Apr 26 '16 at 12:10
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Cursor emailCur = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id},null);
while (emailCur.moveToNext()) {
String email = emailCur.getString( emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email",name+" "+email);
}
emailCur.close();
}
}

- 34,573
- 7
- 66
- 64
Another solution.
private static final Uri URI_CONTACT_DATA = ContactsContract.Data.CONTENT_URI;
private static final String COLUMN_EMAIL = ContactsContract.CommonDataKinds.Email.ADDRESS;
private static final String COLUMN_DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME_PRIMARY;
private static final String COLUMN_MIMETYPE = ContactsContract.Data.MIMETYPE;
private static final String[] PROJECTION = {
COLUMN_DISPLAY_NAME,
COLUMN_EMAIL,
COLUMN_MIMETYPE
};
private Cursor getCursor() {
ContentResolver resolver = context.getContentResolver();
String selection = COLUMN_MIMETYPE + "=?";
final String[] selectionArgs = {ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE};
return resolver.query(URI_CONTACT_DATA, PROJECTION, selection, selectionArgs, null);
}
The issue is that the table at ContactsContract.Contacts.CONTENT_URI
holds the entire contact database. This includes phone numbers, emails, organisations, and even completely custom data, so you cannot use it without filtering with ContactsContract.Data.MIMETYPE
. A row in this database holds a value (or values, it has 15 generic columns) related to a certain account, so you might need to group them yourself. I needed this to autocomplete emails, so the format (email per row) was perfect.

- 4,007
- 5
- 32
- 44
If you can already get the contacts and their email address (if it exists), why don't you just remove the contacts with no email address from your list?
See here for more information on the Android Contacts API.

- 2,834
- 18
- 27
-
Hey thanks IrAndroid. Yes I am doing that only. But I am getting all contact names. Can you please show me some code for that? – Krishna Suthar Apr 12 '12 at 03:42
-
Here's a similar question to what you are asking: http://stackoverflow.com/questions/5205999/android-get-a-cursor-only-with-contacts-that-have-an-email-listed-android-2-0. – lrAndroid Apr 12 '12 at 03:43
-
Hey I tried that code given on this link. But I am getting email address. Not name. I want to get contact names if email address is exist. Can you give me some code? So that I can get some Idea. – Krishna Suthar Apr 12 '12 at 04:37
Here is a simple way to get email id of contact from contact list. You need to pass contact id of user in below method and it will return you email id if exists
public String getEmail(String contactId) {
String emailStr = "";
final String[] projection = new String[]{ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Email.TYPE};
Cursor emailq = managedQuery(ContactsContract.CommonDataKinds.Email.CONTENT_URI, projection, ContactsContract.Data.CONTACT_ID + "=?", new String[]{contactId}, null);
if (emailq.moveToFirst()) {
final int contactEmailColumnIndex = emailq.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
while (!emailq.isAfterLast()) {
emailStr = emailStr + emailq.getString(contactEmailColumnIndex) + ";";
emailq.moveToNext();
}
}
return emailStr;
}
And also if you want to learn how to get contact list in your app follow this link: show contact list in app android - trinitytuts

- 1,072
- 12
- 20
Declare A Global Variable
// Hash Maps
Map<String, String> nameEmailMap = new HashMap<String, String>();
Then Use The Function Below
private void getEmailIDs() {
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);
// Loop Through All The Emails
while (emails.moveToNext()) {
String name = emails.getString(emails.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String email = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
// Enter Into Hash Map
nameEmailMap.put(email, name);
}
// Get The Contents of Hash Map in Log
for (Map.Entry<String, String> entry : nameEmailMap.entrySet()) {
String key = entry.getKey();
Log.d(TAG, "Email :" + key);
String value = entry.getValue();
Log.d(TAG, "Name :" + value);
}
emails.close();
}
Remember in the above example the key is email and value is name so read your contents like mahaXXXX@gmail.com->Mahatma Gandhi instead of Mahatma Gandhi->mahaXXXX@gmail.com

- 3,722
- 2
- 38
- 51