Android snippet of the day - Show Contact Picker

This quickie will get you up and running if you need to display the android built in contact picker to select a phone number with a minimum amount of code.

What's the problem?

Well, after looking for a quick way to select a phone number from within the built in Contact Picker in Android, I stumbled upon a remarkable number of articles with faulty information.

This is what finally worked!

  private void selectContact()
  // This intent will fire up the contact picker dialog
  Intent intent = new Intent(Intent.ACTION_PICK, 
  startActivityForResult(intent, REQUEST_CONTACTPICKER);

If all you wanted to do was to show the contact picker, this would be enough! However I suspect you wish to be notified of which phone number was selected.

Retrieving the result

To actually be notified of the selected result, we need to override the onActivityResult method. This assumes you're implementing the functionality inside an Activity. This is where it can get a bit messy! I'll just post the method and won't bother going into detail as you're probably not interested. If you are, there are plenty of info if you google around a bit.

	 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	  super.onActivityResult(requestCode, resultCode, data);  
	  if (requestCode == REQUEST_CONTACTPICKER)
		if(resultCode == RESULT_OK)
			Uri contentUri = data.getData();
			String contactId = contentUri.getLastPathSegment();
			Cursor cursor = getContentResolver().query(  
			        Phone.CONTENT_URI, null,  
			        Phone._ID + "=?",       // < - Note, not CONTACT_ID!
			        new String[]{contactId}, null);
            Boolean numbersExist = cursor.moveToFirst();            
            int phoneNumberColumnIndex = cursor.getColumnIndex(Phone.NUMBER);            
            String phoneNumber = "";
            while (numbersExist) 
              phoneNumber = cursor.getString(phoneNumberColumnIndex);
           	  phoneNumber = phoneNumber.trim();  
              numbersExist = cursor.moveToNext();
		    if (!phoneNumber.equals("")) 
			} // phoneNumber != ""
		} // Result Code = RESULT_OK
	  } // Request Code = REQUEST_CONTACTPICER
	 }	// end function   	

Basically what this does is open up the contact phone database at the record with the id of the selected number!

In many cases when browsing for a working solution for this, the problem was this statement at line 11: Phone._ID + "=?", the examples I found had specified it as Phone.CONTACT_ID + "=?" which is used to find a specific contact, not a number.

Oh right, the compilation errors

Unless you've already figured it out, you need a few things for this to work.

1. REQUEST_CONTACTPICKER needs to be specified, something like this:

private final static int REQUEST_CONTACTPICKER = 1;

2. You need these imports:

import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Phone;

That's it.

 Android snippet of the day - Show Contact Picker

This quickie will get you up and running if you need to display the android built in contact picker to select a phone number with a minimum amount of code.

 Android Video

After the huge success with Droid Streamer, we were contacted by AndroidVideo.Net and we agreed to develop an app that plays video files.

 Droid Streamer Pro

Droid Streamer Pro is an application for streaming video files directly your Android powered device.

 Free Downloads

All full versions of software previously released by EnkelAdress Software are now available for free.

Page 2 / 2

 Facebook PHP SDK login problem

Have you integrated a facebook login to your site, or are you planning to? There\'s a small bug involving the sign in process that can make your life miserable unless you know how to get around it. Here\'s one possible solution.

 Thumbs Up Immerse VR Headset

This Immerse headset from ThumbsUp offers both positive and negative sides.

 Know your limits: Web Design

You might have the best app ever created, or you might have the user friendliest web page in the world. You might even have the most useful piece of shareware ever made and still not making any money.

 DroidXplorer big in China?

According to an email DroidXplorer will be preinstalled on devices, and be featured on a set of app stores. For free.

 Would this interest you?

Page 1 / 11