Android snippet of the day - Show Contact Picker

Showing the 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, 
           ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
  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.

	 @Override
	 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);
			startManagingCursor(cursor);
            Boolean numbersExist = cursor.moveToFirst();            
            int phoneNumberColumnIndex = cursor.getColumnIndex(Phone.NUMBER);            
            String phoneNumber = "";
            while (numbersExist) 
            {
              phoneNumber = cursor.getString(phoneNumberColumnIndex);
           	  phoneNumber = phoneNumber.trim();  
              numbersExist = cursor.moveToNext();
            }
			stopManagingCursor(cursor);			
		    if (!phoneNumber.equals("")) 
		    {
			  setPhoneNumber(phoneNumber);		    
			} // 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.

Related to "Android snippet of the day - Show Contact Picker"

Know your limits: Web Design

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.

Read Me...

About

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

Read Me...

User friendly apps, Dialogs part 1

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

Read Me...

Free Icons and Glyphs for your apps or web sites

Why these

Simply put, most graphic resources aren't free if you wish to distribute them with your apps or use them on your sites.

Mind you, I'm no graphic artists and can barely find my way through Paint Shop Pro. The only reason I post these resources is because somewhere, sometime there might be a person who is actually worse than me in producing graphics. These icons and resources are for them.

Use however you wish, whenever you want.

I will add to this article whenever I create new resources for my apps or sites

Read Me...
Page 1 / 3
Previous Next


Just In

Know your limits: Web Design

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.

Read Me...

DroidXplorer big in China?

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.

Read Me...

QuickTip: PayPal IPN - Complex Pass Through Data

QuickTip: PayPal IPN - Complex Pass Through Data

Send complex data in the custom field that you use to match a payment with a customer, order or whatever you need.

Read Me...

PayPal IPN - Take control over your incoming payments!

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.

Read Me...

DroidStop Android Apps

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

Read Me...

QualityApp.net 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..

Read Me...

Alpha Magnetic Spectrometer

Two years after the final mission of Endeavour and the ending of the space shuttle era, the intrument called "Alphamagnetic Spectrometer" may have something to tell us.

Read Me...

Smart Syncing between your desktop and box.net, dropbox and other cloud storage services

Recently I came across a good deal for an online storage service and thought it would be a good idea to sync photos and videos to it so I wouldn't be dependent on a single physical computer.

Read Me...
Page 1 / 6
Previous Next