Read Contacts from device


This is a sample activity which shows How to read the contacts of phone and display them in your application. Last topic published on this forum is Detect USB connection.

Underlying Algorithm:

Basic description of algorithm in step by step form:
1.) Create a Project ReadContacts.
2.) Put the following code snippet in res/layout/main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:id ="@+id/con"

3.) Add the following permission in AndroidManifest.xml :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    <uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ReadContacts"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />


4.) Import the following packages in activity:

import android.database.Cursor;
import android.provider.ContactsContract;
import android.widget.TextView;

5.) Add some contacts in device/emulator.
6.) Run the Application.

Steps to Create:

1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. ReadContacts. Enter following information:
Project name: ReadContacts
Build Target: Android APIs2.1
Application name: ReadContacts
Package name: ReadContacts
Create Activity: ReadContacts

On Clicking Finish ReadContacts code structure is generated with the necessary Android Packages being imported along with ReadContacts class will look like following:


import android.os.Bundle;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.widget.TextView;

public class ReadContacts extends Activity {
	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		TextView contactView = (TextView) findViewById(;

		Cursor cursor = getContacts();

		while (cursor.moveToNext()) {
			String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
			contactView.append("Name: ");

	private Cursor getContacts() {
		// Run query
		Uri uri = ContactsContract.Contacts.CONTENT_URI;
		String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };
		String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'";
		String[] selectionArgs = null;
		String sortOrder = ContactsContract.Contacts.DISPLAY_NAME+ " COLLATE LOCALIZED ASC";
		return managedQuery(uri, projection, selection, selectionArgs, sortOrder);

Output –The final output:

Did you enjoy this article?
Get Free Updates

9 Responses

  1. A SUPPORTED BY THE DEVELOPER TOOLS? It was interesting. You seem very knowledgeable in your field.

  2. Nil says:

    But how this will work on 1.5 or 1.6? Does Android SDK adjusts itself.

  3. nakul says:

    it is showing force close

    wat the solution??????????????

  4. Krunal Doshi says:

    The method managedQuery(Uri, String[], String, String[], String) from the type Activity is deprecated

  5. pissed says:

    How can i loop and change all contacts accordingly… got +600 contacts, and providers here were so smart to add a new digit, but not providing a solution to change those numbers

  6. Gobinath says:

    yes its working, but i cant scroll the window after it displays all contacts. why?

  7. krunal says:

    nice tutorial

  8. ash says:

    can you please give us code for getting all contacts and all info of each contact

  9. minahal says:

    it is possible to display phone numbers also with their names?

Leave a Reply

− 4 = one

Proudly designed by Theme Junkie.