I am a beginner to android, i am building a application in which when the user presses a button, the contacts which is stored in the mobile are shown. When he selects a cont
Simplest way I've found to pick a contact with phone number, and show display name and the selected phone number. See also: full, executable example as gist.
private final static int PICK_CONTACT = 0;
private void pickContact() {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
// Show only contacts with phone number. (Also forces user to choose ONE
// phone number for a contact that has several.)
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
handleSelectedContact(data);
}
break;
}
}
private void handleSelectedContact(Intent intent) {
Uri contactUri = intent.getData();
Cursor c = getContentResolver().query(contactUri, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String selectedPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.Entity.DATA1));
String message = String.format("Name %s, selected phone %s", name, selectedPhone);
Snackbar.make(contentView, message, Snackbar.LENGTH_LONG).show();
}
c.close();
}
If you need other fields, for example both phone number and email, check out answers in this question.
Start the Contact Activity,
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
Now use the following to get all the data,
@Override
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 = managedQuery(contactData, null, null, null, null);
ContentResolver cr = getContentResolver();
if (c.moveToFirst())
{
try
{
String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
String phone = "";
while (pCur.moveToNext())
{
try
{
phone = phone + pCur.getString(pCur.getColumnIndex(ContactsContract.Contacts.Data.DATA1)) + ",";
}
catch(Exception ex)
{
}
}
pCur.close();
if(phone.length() > 0)
{
phone = phone.substring(0,phone.length()-1);
}
txtPhone.setText(phone);
}
txtName.setText(name);
}
catch(Exception ex)
{
Toast.makeText(AddToInnerCircle.this, "No name selected", Toast.LENGTH_SHORT).show();
}
}
}
}
}
public void readcontact(){
try {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
PhoneLookup.CONTENT_FILTER_URI); // creates the contact list intent
contactPickerIntent.setType(Contacts.Phones.CONTENT_TYPE); // selects contacts with phone only
startActivityForResult(contactPickerIntent, PICK_CONTACT);
} catch (Exception e) {
e.printStackTrace();
}
}
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(); // has the uri for picked contact
Cursor c = getContentResolver().query(contactData, null, null, null, null); // creates the contact cursor with the returned uri
if (c.moveToFirst()) {
String name = c..getString(c.getColumnIndex(PhoneLookup.DISPLAY_NAME));
String number = c.getString(c.getColumnIndex(PhoneLookup.NUMBER));
Toast.makeText(this, name + " has number " + number, Toast.LENGTH_LONG).show();
}
}
break;
}
}
Here i will demonstrate you that how to pick a contact no and name from a contact list on click or focus event,
Focus Event:-
phoneNo.setOnFocusChangeListener(new OnFocusChangeListener()
{ public void onFocusChange(View v, boolean hasFocus)
{
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent,PICK_CONTACT );//PICK_CONTACT is private static final int, so declare in activity class
} });
A FUNCTION TO GET THE CONTACT NAME AND PHONE NO IS :
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 = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id =
c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone =
c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,
null, null);
phones.moveToFirst();
String phn_no = phones.getString(phones.getColumnIndex("data1"));
String name = c.getString(c.getColumnIndex(StructuredPostal.DISPLAY_NAME));
Toast.makeText(this, "contact info : "+ phn_no+"\n"+name, Toast.LENGTH_LONG).show();
}
}
}
}
}
Intent intent1 = new Intent(Intent.ACTION_PICK, Contacts.Phones.CONTENT_URI);
startActivityForResult(intent1, PICK_CONTACT_RQCODE_OLD);
startActivity(intent1);
This works in my implementation.
Refer Contacts Application which comes with Android Source