I need to be able to select multiple contacts in Android. The flow is like this :
- User clicks on a button which opens the Contacts application.
- However, instead of being able to select a single contact, I need to be able to select multiple contacts (in the same launch of the intent).
- If a contact has multiple phone numbers, I need the user to be able to choose which phone number he wants to select.
This feature is already present in my Samsung Android Phone (Running 2.3 Gingerbread) when I click on "Contacts" in the Messaging app. See screenshot below :
There is not built in way of doing this, so you need to do most of the work by yourself. Luckily, it's not that hard.
Display
To Display your contacts you can use either a listview with the multi-select choice mode, or you can create a custom adapter and bind it to a regular listview. I don't think the listview with multi-select will let you put anything other than text for each row, but you'd have to dig deeper to find out.
I've used the custom adapter method for something very similar (except the multiple phone numbers part). It's pretty easy to do and I find custom adapters are really useful in the long run.
Custom Adapter Listview Tutorial
With a custom adapter setup, you can create data objects with all the information for a person including their Name and Phone Number(s). In the getView of your Custom Adapter, you can decide what/how and where to display each piece of information.
Gathering Information
You'll need to use the ContactContract API to get information for your contacts.
Reading Contact Info
Reading ALL phone numbers for a Contact
You will have to write this all yourself. You can use the ContactsContract
provider to query for all contacts with phone numbers, and then for the selected contact you can query for all phone numbers for that contact. You can display the results in activities or dialogs as you see fit.
Unfortunately this code isn't supported for all versions of android
I know it's kinda late but wanted to share this!
I found some incomplete code in the net and after cracking my head with it I finally found the answer!
Basically you launch the picker and let it return the data in the extras =]
There was no full answer in the net so hope it helps to some soul out there!
Enjoy:
public void pickContact(View v){
try {
Intent phonebookIntent = new Intent("intent.action.INTERACTION_TOPMENU");
phonebookIntent.putExtra("additional", "phone-multi");
startActivityForResult(phonebookIntent, PICK_CONTACT);
// PICK_CONTACT IS JUST AN INT HOLDING SOME NUMBER OF YOUR CHOICE
} catch (Exception e) {
e.printStackTrace();
}
}
public String getData(String contact, int which)
{
return contact.split(";")[which];
}
public void onActivityResult(int reqCode, int resultCode, Intent data) {
final int URI = 0;
final int NUMBER = 1;
if (RESULT_OK != resultCode) return;
Bundle contactUri = data.getExtras();
if (null == contactUri) return;
ArrayList<String> contacts = (ArrayList<String>)contactUri.get("result");
Toast.makeText(getApplicationContext(), getData(contacts.get(0),NUMBER), Toast.LENGTH_SHORT).show();
}