How to call Android contacts list?

AndroidContacts

Android Problem Overview


I'm making an Android app, and need to call the phone's contact list. I need to call the contacts list function, pick a contact, then return to my app with the contact's name. Here's the code I got on the internet, but it doesnt work.

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class Contacts extends ListActivity {
	
	private ListAdapter mAdapter;
	public TextView pbContact;
	public static String PBCONTACT;
	public static final int ACTIVITY_EDIT=1;
	private static final int ACTIVITY_CREATE=0;
	
	// Called when the activity is first created. 
	@Override
	public void onCreate(Bundle icicle) {
    	super.onCreate(icicle);
    	Cursor C = getContentResolver().query(People.CONTENT_URI, null, null, null, null);
    	startManagingCursor(C);

    	String[] columns = new String[] {People.NAME};
    	int[] names = new int[] {R.id.row_entry};

    	mAdapter = new SimpleCursorAdapter(this, R.layout.mycontacts, C, columns, names);
    	setListAdapter(mAdapter);
	} // end onCreate()
    // Called when contact is pressed
	@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        
        Cursor C = (Cursor) mAdapter.getItem(position);
        PBCONTACT = C.getString(C.getColumnIndex(People.NAME));
        
        // RHS 05/06
        //pbContact = (TextView) findViewById(R.id.myContact);
        //pbContact.setText(new StringBuilder().append("b"));
        
        Intent i = new Intent(this, NoteEdit.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }
}

Android Solutions


Solution 1 - Android

I'm not 100% sure what your sample code is supposed to do, but the following snippet should help you 'call the contacts list function, pick a contact, then return to [your] app with the contact's name'.

There are three steps to this process.

#1. Permissions

Add a permission to read contacts data to your application manifest.

<uses-permission android:name="android.permission.READ_CONTACTS"/>

#2. Calling the Contact Picker

Within your Activity, create an Intent that asks the system to find an Activity that can perform a PICK action from the items in the Contacts URI.

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

Call startActivityForResult, passing in this Intent (and a request code integer, PICK_CONTACT in this example). This will cause Android to launch an Activity that's registered to support ACTION_PICK on the People.CONTENT_URI, then return to this Activity when the selection is made (or canceled).

startActivityForResult(intent, PICK_CONTACT);

#3. Listening for the Result

Also in your Activity, override the onActivityResult method to listen for the return from the 'select a contact' Activity you launched in step 2. You should check that the returned request code matches the value you're expecting, and that the result code is RESULT_OK.

You can get the URI of the selected contact by calling getData() on the data Intent parameter. To get the name of the selected contact you need to use that URI to create a new query and extract the name from the returned cursor.

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
  super.onActivityResult(reqCode, resultCode, data);
  
  switch (reqCode) {
    case (PICK_CONTACT) :
      if (resultCode == Activity.RESULT_OK) {
        Uri contactData = data.getData();
        Cursor c =  getContentResolver().query(contactData, null, null, null, null);
        if (c.moveToFirst()) {
          String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
          // TODO Whatever you want to do with the selected contact name.
        }
      }
      break;
  }
}

Full source code: tutorials-android.blogspot.com (how to call android contacts list).

Solution 2 - Android

I do it this way for Android 2.2 Froyo release: basically use eclipse to create a class like: public class SomePickContactName extends Activity

then insert this code. Remember to add the private class variables and CONSTANTS referenced in my version of the code:

protected void onCreate(Bundle savedInstanceState) 
{
  super.onCreate(savedInstanceState);      	
  Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
  startActivityForResult(intentContact, PICK_CONTACT);
}//onCreate

public void onActivityResult(int requestCode, int resultCode, Intent intent) 
{

  if (requestCode == PICK_CONTACT)
  {    		
    getContactInfo(intent); 		
    // Your class variables now have the data, so do something with it. 
  }
}//onActivityResult

protected void getContactInfo(Intent intent)
{
	
   Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);      
   while (cursor.moveToNext()) 
   {      	   
	   String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
	   name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 

	   String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

	   if ( hasPhone.equalsIgnoreCase("1"))
		   hasPhone = "true";
	   else
		   hasPhone = "false" ;
	   
	   if (Boolean.parseBoolean(hasPhone)) 
	   {
	    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
	    while (phones.moveToNext()) 
	    {
	      phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
	    }
	    phones.close();
	   }
	   
	   // Find Email Addresses
	   Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null);
	   while (emails.moveToNext()) 
	   {
	    emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
	   }
	   emails.close();

	Cursor address = getContentResolver().query(
	            ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
	            null,
	            ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId,
	            null, null);
    while (address.moveToNext()) 
    { 
      // These are all private class variables, don't forget to create them.
      poBox      = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
      street     = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
      city       = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
      state      = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
      postalCode = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
      country    = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
      type       = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
    }  //address.moveToNext()	
  }  //while (cursor.moveToNext())        
   cursor.close();
}//getContactInfo

Solution 3 - Android

Looking around for an API Level 5 solution using ContactsContract API you could slightly modify the code above with the following:

  Intent intent = new Intent(Intent.ACTION_PICK);
  intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
  startActivityForResult(intent, PICK_CONTACT);

And then in onActivityResult use the column name:

  ContactsContract.Contacts.DISPLAY_NAME

Solution 4 - Android

Here is the code snippet for get contact:

package com.contact;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class GetContactDemoActivity extends Activity implements OnClickListener {

private Button btn = null;
private TextView txt = null;

@Override
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main);

	btn = (Button) findViewById(R.id.button1);
	txt = (TextView) findViewById(R.id.textView1);

	btn.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
	if (arg0 == btn) {
		try {
			Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
			startActivityForResult(intent, 1);
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("Error in intent : ", e.toString());
		}
	}
}

@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
	super.onActivityResult(reqCode, resultCode, data);

	try {
		if (resultCode == Activity.RESULT_OK) {
			Uri contactData = data.getData();
			Cursor cur = managedQuery(contactData, null, null, null, null);
			ContentResolver contect_resolver = getContentResolver();

			if (cur.moveToFirst()) {
				String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
				String name = "";
				String no = "";

				Cursor phoneCur = contect_resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
						ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);

				if (phoneCur.moveToFirst()) {
					name = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
					no = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				}

				Log.e("Phone no & name :***: ", name + " : " + no);
				txt.append(name + " : " + no + "\n");

				id = null;
				name = null;
				no = null;
				phoneCur = null;
			}
			contect_resolver = null;
			cur = null;
			//						populateContacts();
		}
	} catch (IllegalArgumentException e) {
		e.printStackTrace();
		Log.e("IllegalArgumentException :: ", e.toString());
	} catch (Exception e) {
		e.printStackTrace();
		Log.e("Error :: ", e.toString());
	}
}

}

Solution 5 - Android

	    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
	    while (phones.moveToNext())
		{
	    String Name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
	    String Number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
	    
		}

Solution 6 - Android

The complete code is given below

    package com.testingContect;

    import android.app.Activity;
    import android.content.Intent;
    import android.database.Cursor;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.provider.Contacts.People;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    public class testingContect extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
	
	EditText ed;
	Button bt;
	int PICK_CONTACT;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bt=(Button)findViewById(R.id.button1);
        ed =(EditText)findViewById(R.id.editText1);
        ed.setOnClickListener(this);
        bt.setOnClickListener(this);
        
        
    }

	@Override
	public void onClick(View v) {
		
		switch(v.getId())
		{
		case R.id.button1:
			
			break;
		case R.id.editText1:
			 Intent intent = new Intent(Intent.ACTION_PICK);
			  intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
			  startActivityForResult(intent, PICK_CONTACT);


			break;
			
		}
		
	}
	public void onActivityResult(int requestCode, int resultCode, Intent intent) 
	{

	  if (requestCode == PICK_CONTACT)
	  {         
		  Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);
		  cursor.moveToNext();
		  String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
	       String  name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
	       
		  Toast.makeText(this, "Contect LIST  =  "+name, Toast.LENGTH_LONG).show(); 
	  }
	}//onActivityResult
}//class ends

Solution 7 - Android

public void onActivityResult(int requestCode, int resultCode, Intent intent) 
{
  if (requestCode == PICK_CONTACT && intent != null) //here check whether intent is null R not
  {  
  }
}

because without selecting any contact it will give an exception. so better to check this condition.

Solution 8 - Android

To my surprise you do not need users-permission CONTACT_READ to read the names and some basic information (Is the contact starred, what was the last calling time). However you do need permission to read the details of the contact like phone number.

Solution 9 - Android

Be careful while working with android contact list.

Reading contact list in above methods work on most android devices except HTC One and Sony Xperia. It wasted my six weeks trying to figure out what is wrong!

Most tutorials available online are almost similar - first read "ALL" contacts, then show in Listview with ArrayAdapter. This is not memory efficient solution. Instead of looking for solutions on other websites first, have a look at developer.android.com. If any solution is not available on developer.android.com you should look somewhere else.

The solution is to use CursorAdapter instead of ArrayAdapter for retrieving contact list. Using ArrayAdapter would work on most devices, but it's not efficient. The CursorAdapter retrieves only a portion of contact list at run time while the ListView is being scrolled.

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ...
    // Gets the ListView from the View list of the parent activity
    mContactsList =
        (ListView) getActivity().findViewById(R.layout.contact_list_view);
    // Gets a CursorAdapter
    mCursorAdapter = new SimpleCursorAdapter(
            getActivity(),
            R.layout.contact_list_item,
            null,
            FROM_COLUMNS, TO_IDS,
            0);
    // Sets the adapter for the ListView
    mContactsList.setAdapter(mCursorAdapter);
}

Retrieving a List of Contacts: Retrieving a List of Contacts

Solution 10 - Android

hi i have a code to save the contact in your database by shared preference here is my code

public class Main22Activity extends AppCompatActivity {
    EditText nameInput,phoneInput;
    TextView LargeText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main22);
        nameInput = (EditText) findViewById(R.id.nameInput);
        phoneInput = (EditText) findViewById(R.id.phoneInput);

        LargeText = (TextView) findViewById(R.id.textView2);
    }

    public void saveInfo (View view){
        SharedPreferences sharedPref = getSharedPreferences("nameInfo" , Context.MODE_PRIVATE);
        SharedPreferences.Editor editor= sharedPref.edit();
        editor.putString("name", nameInput.getText().toString());
        editor.putString("phone", phoneInput.getText().toString());
        editor.apply();
        Toast.makeText(this, "Saved", Toast.LENGTH_LONG).show();
    }

    public void displayData(View view){
        SharedPreferences sharedPref = getSharedPreferences("nameInfo" , Context.MODE_PRIVATE);
        String name = sharedPref.getString("name", "");
        String ph = sharedPref.getString ("phone","");
        LargeText.setText(name + " " + ph);
    }
}

Solution 11 - Android

>I am using this method to read Contacts

public static List<ContactItem> readPhoneContacts(Context context) {
        List<ContactItem> contactItems = new ArrayList<ContactItem>();
        try {
            Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
                    null, null, "upper("+ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ") ASC");

            /*context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?",
                    new String[] { id },
                    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");*/

            Integer contactsCount = cursor.getCount(); // get how many contacts you have in your contacts list
            if (contactsCount > 0) {
                while (cursor.moveToNext()) {

                    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                        ContactItem contactItem = new ContactItem();
                        contactItem.setContactName(contactName);
                        //the below cursor will give you details for multiple contacts
                        Cursor pCursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                                new String[]{id}, null);
                        // continue till this cursor reaches to all phone numbers which are associated with a contact in the contact list
                        while (pCursor.moveToNext()) {
                            int phoneType = pCursor.getInt(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                            //String isStarred 		= pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED));
                            String phoneNo = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            //you will get all phone numbers according to it's type as below switch case.
                            //Logs.e will print the phone number along with the name in DDMS. you can use these details where ever you want.
                            switch (phoneType) {
                                case Phone.TYPE_MOBILE:
                                    contactItem.setContactNumberMobile(phoneNo);
                                    Log.e(contactName + ": TYPE_MOBILE", " " + phoneNo);
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                    contactItem.setContactNumberMobile(phoneNo);
                                    Log.e(contactName + ": TYPE_HOME", " " + phoneNo);
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                    contactItem.setContactNumberMobile(phoneNo);
                                    Log.e(contactName + ": TYPE_WORK", " " + phoneNo);
                                    break;
                                case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
                                    contactItem.setContactNumberMobile(phoneNo);
                                    Log.e(contactName + ": TYPE_WORK_MOBILE", " " + phoneNo);
                                    break;
                                case Phone.TYPE_OTHER:
                                    contactItem.setContactNumberMobile(phoneNo);
                                    Log.e(contactName + ": TYPE_OTHER", " " + phoneNo);
                                    break;
                                default:
                                    break;
                            }
                        }
                        contactItem.setSelectedAddress(getContactPostalAddress(pCursor));
                        pCursor.close();
                        contactItems.add(contactItem);
                    }

                }
                cursor.close();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }


        return contactItems;
    }//loadContacts

Solution 12 - Android

-> Add a permission to read contacts data to your application manifest.

<uses-permission android:name="android.permission.READ_CONTACTS"/>

-> Use Intent.Action_Pick in your Activity like below

Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
        startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT);

-> Then Override the onActivityResult()

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // check whether the result is ok
        if (resultCode == RESULT_OK) {
            // Check for the request code, we might be usign multiple startActivityForReslut
            switch (requestCode) {
            case RESULT_PICK_CONTACT:
               Cursor cursor = null;
        try {
            String phoneNo = null ;
            String name = null;           
            Uri uri = data.getData();            
            cursor = getContentResolver().query(uri, null, null, null, null);
            cursor.moveToFirst();           
            int  phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            phoneNo = cursor.getString(phoneIndex); 
           
            textView2.setText(phoneNo);
        } catch (Exception e) {
            e.printStackTrace();
        }
                break;
            }
        } else {
            Log.e("MainActivity", "Failed to pick contact");
        }
    }

This will work check it out

Solution 13 - Android

I use the code provided by @Colin MacKenzie - III. Thanks a lot!

For someone who are looking for a replacement of 'deprecated' managedQuery:

1st, assuming using v4 support lib:

import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;

2nd:

your_(activity)_class implements LoaderManager.LoaderCallbacks<Cursor>

3rd,

// temporarily store the 'data.getData()' from onActivityResult
private Uri tmp_url;

4th, override callbacks:

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // create the loader here!
    CursorLoader cursorLoader = new CursorLoader(this, tmp_url, null, null, null, null);
    return cursorLoader;
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    getContactInfo(cursor); // here it is!
}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
}

5th:

public void initLoader(Uri data){
    // will be used in onCreateLoader callback
    this.tmp_url = data;
    // 'this' is an Activity instance, implementing those callbacks
    this.getSupportLoaderManager().initLoader(0, null, this);
}

6th, the code above, except that I change the signature param from Intent to Cursor:

protected void getContactInfo(Cursor cursor)
{

   // Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);      
   while (cursor.moveToNext()) 
   {
        // same above ...
   } 

7th, call initLoader:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (PICK_CONTACT == requestCode) {
        this.initLoader(data.getData(), this);
    }
}

8th, don't forget this piece of code

    Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    this.act.startActivityForResult(intentContact, PICK_CONTACT);

References:

Android Fundamentals: Properly Loading Data

Initializing a Loader in an Activity

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTam N.View Question on Stackoverflow
Solution 1 - AndroidReto MeierView Answer on Stackoverflow
Solution 2 - AndroidColin MacKenzie - IIIView Answer on Stackoverflow
Solution 3 - AndroidDaddyboyView Answer on Stackoverflow
Solution 4 - AndroidMayur BholaView Answer on Stackoverflow
Solution 5 - AndroidMaidulView Answer on Stackoverflow
Solution 6 - AndroidANDROID CONTECT LISTView Answer on Stackoverflow
Solution 7 - AndroidKiran KalaView Answer on Stackoverflow
Solution 8 - AndroidVicky SinghView Answer on Stackoverflow
Solution 9 - AndroidAdnanView Answer on Stackoverflow
Solution 10 - AndroidAbhishekView Answer on Stackoverflow
Solution 11 - AndroidZar E AhmerView Answer on Stackoverflow
Solution 12 - AndroidRaviView Answer on Stackoverflow
Solution 13 - AndroidYangView Answer on Stackoverflow