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"?-->
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:id ="@+id/con"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

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

<!--?xml version="1.0" encoding="utf-8"?-->
      package="com.app.ReadContacts"
      android:versionCode="1"
      android:versionName="1.0">
    

    
                          android:label="@string/app_name">
            
                
                
            
        

    

4.) Import the following packages in activity:

import android.database.Cursor;
import android.net.Uri;
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: com.app. ReadContacts
Create Activity: ReadContacts

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

package com.app.ReadContacts;

import android.app.Activity;
import android.os.Bundle;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.widget.TextView;

public class ReadContacts extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		TextView contactView = (TextView) findViewById(R.id.con);

		Cursor cursor = getContacts();

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

	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:

About the Author

Leave a Reply 11 comments

Tyson F. Gautreaux - July 25, 2011 Reply

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

Nil - August 8, 2011 Reply

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

nakul - April 12, 2012 Reply

it is showing force close

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

Krunal Doshi - September 30, 2012 Reply

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

pissed - October 1, 2012 Reply

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

Gobinath - February 3, 2013 Reply

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

krunal - May 7, 2013 Reply

nice tutorial

ash - May 31, 2013 Reply

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

minahal - July 27, 2013 Reply

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

Leave a Reply: