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, 
           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.

 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.

 About

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

 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