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.

 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.


A little about me, and what this page will be about from now on.

 User friendly apps, Dialogs part 1

In this first article about how to design user friendly apps we will discuss the use of Dialogs.

 Android snippet of the day - Simple Http Post request

So, what we want is this:

  • Call a web server asynchronously
  • Display a loading dialog during the time it executes
  • Present the response html.

This code could easily be used as a base if you need to call a web service that outputs xml

Page 1 / 2

 PayPal IPN - Take control over your incoming payments!

I'm sure you've heard all about the simplicity of using a payment processor service to sell your items online, but don't miss out on the ones who prefer using Paypal. App Review Index

Having your app reviewed is a great way to boost your downloads! Read more on how QualityApp can help you find your mentions..

 DroidStop Android Apps

Despite popular belief, creating mobile apps and games is NOT the quick way to the millions!

 Internet Explorer 9, JScript Error c00c023f

Ajax and Error c00c023f

After updating to Internet Explorer 9, a previously working ajax driven site of mine stopped working sporadically.

In this short article you will see how to reproduce, and how to solve the problem.

 Would this interest you?

Page 2 / 11