This will open contact to select a number
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
Now use onActivityResult
to get the contact
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
String number = c.getString(c.getColumnIndexOrThrow(People.NUMBER));
perrsonname.setText(name);
Toast.makeText(this, name + " has number " + number, Toast.LENGTH_LONG).show();
}
}
break;
}
}
Don't forget to add the Read Contacts permission.
update :-
inside your oncreate()
add the following and remove the previous code from my answer.
Button addNewContact = (Button) findViewById(R.id.addNewContact);
addNewContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, CHOOSE_CONTACTS);
}
});
remove code from comment get contacts
to end contacts
.