How can I tell how many contacts there are in the contact list? I got the contact number, but one person can have more than one contact and I want to account for this in finding the total number of contacts in the contact list.
4 Answers
To find the count of phone numbers of all the contacts
Cursor cursor = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
int count = cursor.getCount();
To find the count of all the phone numbers of a particular RawContactID (pass the contact id value in rawContactId).
Cursor cursor = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID + " = " + rawContactId, null, null);
int count = cursor.getCount();
The number of contacts displayed in the ContactsListActivity consists can be determined by following query.
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
int count = cursor.getCount();
However if a person has been entered under multiple accounts then only a single instance is obtained by the above query as ContactsContract.Contacts combines all such contacts.
Cursor cursor = managedQuery(RawContacts.CONTENT_URI, null, null, null, null);
int count = cursor.getCount();
The relation between ContactsContract.Contacts and RawContacts can be found out at http://developer.android.com/resources/articles/contacts.html
Hope this resolves your doubt!

- 3,027
- 2
- 29
- 37
-
sorry but it dosent work.it is giving me that also which dosent have contact no., which have only email ids. so can we have total number of phone numbers which are stored in contact list? – nimi Nov 27 '10 at 08:38
-
Ya this code will return all the contacts.I am not able to get the exact requirnment. I had answered the following question "How can I tell how many contacts there are in the contact list?" Do you need to find the total number of phone numbers of all the contacts? In that case i am editing my answer. – Manish Khot Nov 27 '10 at 10:14
A really old thread, but if you want to count contacts WITH phone numbers you can use this:
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts.HAS_PHONE_NUMBER, null, null);
int count = cursor.getCount();
Of course managedQuery is deprecated now, but this can help in a bind :)

- 1,069
- 12
- 11
As we know managedQuery is now deprecated we can do this task like this
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
int contactNewCount = cursor.getCount();
cursor.close();

- 173
- 1
- 6
If anybody needs still the answer:
managedQuery
is now deprecated, so we'll be using query
instead from getContentResolver()
.
int contactCount = 0;
Cursor cursor = getContentResolver().query(android.provider.ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cursor != null) {
contactCount = cursor.getCount();
cursor.close();
}
Note: This answer is tested on Android 10, 11 and 12.

- 1,034
- 2
- 12
- 26