Reputation: 5083
I am starting contact picker activity to get phone number
val i = Intent(Intent.ACTION_PICK)
i.type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE
startActivityForResult(i, REQUEST_CODE_PICK_CONTACT)
If contact has no default number, phone number picker dialog is shown
If a contact has default number, phone number picker dialog is not shown and default number is taken by default.
So my question: How to show phone picker dialog even if a contact has default number?
Upvotes: 1
Views: 116
Reputation: 28169
Instead of using the Phone-Picker, use the Contact-Picker, and show the phones dialog yourself.
Intent intent = Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_SELECT_CONTACT);
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_SELECT_CONTACT && resultCode == RESULT_OK) {
Uri contactUri = data.getData();
long contactId = getContactIdFromUri(contactUri);
List<String> phones = getPhonesFromContactId(contactId);
showPhonesDialog(phones);
}
}
private long getContactIdFromUri(Uri contactUri) {
Cursor cur = getContentResolver().query(contactUri, new String[]{ContactsContract.Contacts._ID}, null, null, null);
long id = -1;
if (cur.moveToFirst()) {
id = cur.getLong(0);
}
cur.close();
return id;
}
private List<String> getPhonesFromContactId(long contactId) {
Cursor cur = getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI,
new String[]{CommonDataKinds.Phone.NUMBER},
CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{String.valueOf(contactId)}, null);
List<String> phones = new ArrayList<>();
while (cur.moveToNext()) {
String phone = cur.getString(0);
phones.add(phone);
}
cur.close();
return phones;
}
private void showPhonesDialog(List<String> phones) {
String[] phonesArr = phones.toArray(new String[0]);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Select a phone:");
builder.setItems(phonesArr, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("Phone Selection", "user selected: " + phonesArr[which]);
}
});
builder.show();
}
Upvotes: 2