Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting a number from user with multiple numbers when using the contact picker

I'm trying to allow a user to select a phone number from a contact using the contact picker. However, right now all the examples I see online show how you can select a contact, but I am hoping to have a second screen then pop up if that contact has multiple phone numbers so you can specify which one you want to select (the way that text message lets you do so when you select a contact).

My question is, do you have to gather all of the numbers and then ask the user to select a number, or is this functionality already built into Android? I'm hoping I just forgot a flag or something.

like image 645
Kevin Avatar asked May 30 '11 00:05

Kevin


People also ask

How do you use contact picker?

Opening the Contact Picker #contacts. select() . When called, it returns a promise and shows the contact picker, allowing the user to select the contact(s) they want to share with the site. After selecting what to share and clicking Done, the promise resolves with an array of contacts selected by the user.

Can I use contact picker API?

Most mobile devices support Contact Picker API except only IOS Safari and Firefox on Android. On desktops Chrome, Opera and Edge already support this feature.


1 Answers

I was able to do this by creating a second dialog which shows all the phone numbers associated with a contact. First, call this somewhere in your code:

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, REQUEST_PICK_CONTACT);

Then in onActivityResult() use this to decide if the selected contact has multiple phone numbers, and display a dialog if so:

Uri result = data.getData();
Log.v(TAG, "Got a result: " + result.toString());

// get the contact id from the Uri
String id = result.getLastPathSegment();

// query for phone numbers for the selected contact id
c = getContentResolver().query(
    Phone.CONTENT_URI, null,
    Phone.CONTACT_ID + "=?",
    new String[]{id}, null);

int phoneIdx = c.getColumnIndex(Phone.NUMBER);
int phoneType = c.getColumnIndex(Phone.TYPE);

if(c.getCount() > 1) { // contact has multiple phone numbers
    final CharSequence[] numbers = new CharSequence[c.getCount()];
    int i=0;
    if(c.moveToFirst()) {
        while(!c.isAfterLast()) { // for each phone number, add it to the numbers array
            String type = (String) Phone.getTypeLabel(this.getResources(), c.getInt(phoneType), ""); // insert a type string in front of the number
            String number = type + ": " + c.getString(phoneIdx);
            numbers[i++] = number;
            c.moveToNext();
        }
        // build and show a simple dialog that allows the user to select a number
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(R.string.select_contact_phone_number_and_type);
        builder.setItems(numbers, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int item) {
                String number = (String) numbers[item];
                int index = number.indexOf(":");
                number = number.substring(index + 2);
                loadContactInfo(number); // do something with the selected number
            }
        });
        AlertDialog alert = builder.create();
        alert.setOwnerActivity(this);
        alert.show();

    } else Log.w(TAG, "No results");
} else if(c.getCount() == 1) {
    // contact has a single phone number, so there's no need to display a second dialog
}

I know this is an old question but I hope it helps.

like image 82
howettl Avatar answered Sep 20 '22 08:09

howettl