Often in Android development, there is need to support the following workflow:
- Display list of contacts
- User selects a contact
- Display contact details
- User selects a phone number
- Perform an action with the number
There is a standard way to call the contact list in Android, but that method pulls up a new full-screen activity for showing the contact list. When the user selects a contact, it returns the contact ID to your activity and you can do what you wish with it. However, this method does not always feel well-integrated in your app and can reduce the quality of the user experience. This is especially true for tabbed Android applications where the contact picker needs to remain embedded within the tab.
I was surprised that an API for enabling this kind of workflow was not available on Android. Searching on StackOverflow and other forums clearly show the need. So while developing CallingProxy, I had to roll a contact picker which allows the workflow enumerated in the steps above. I extracted it from the app and am making available in the hope that someone might find use for it.
The Contact Picker uses the new Fragments API in Android and supports Android API level 7 (Froyo) and above. The code for the contact picker is available on GitHub under an MIT licence. So feel free to get hacking and use it for your projects. One known issue is that the Contact Picker does not work well in landscape mode. There is a problem with switching the fragments for the different views in landscape mode. Send me a pull request on GitHub to fix it. 😉
For now, the Contact Picker only supports picking phone numbers, but can easily be extended to support email and other types of contact info. Hopefully I’ll get around to that sometime.
For now, for those who need only phone numbers, Happy hacking!