I am developing a gps tracking app in android. I am done with displaying the map n stuff. Now I want to make a button on top which when clicked would display the contacts, Then when I select the contact it should show me his or her location. Please help me with this. Thank you.
Asked
Active
Viewed 3.8k times
8
-
Pretty sure this is the same question: http://stackoverflow.com/questions/4992564/open-device-contacts-list-at-button-click-event – NotACleverMan Mar 31 '12 at 12:27
-
thanks.. but where do i define that code. – Mehul Rastogi Mar 31 '12 at 12:35
-
http://stackoverflow.com/questions/9766263/getting-contact-number-using-content-provider-in-android/9883740#9883740 check this would help you – Avi Kumar Mar 31 '12 at 12:42
3 Answers
20
You can set an Event on Button click by setting an OnClickListener
on the Button with the following code, and use Intent to call ContactPicker activity:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent= new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
and in onActivityResult()
process the contact uri to load details of 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) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Fetch other Contact details as you want to use
}
}
break;
}
}

farhan patel
- 1,490
- 2
- 19
- 30

jeet
- 29,001
- 6
- 52
- 53
-
-
What is managedQuery, where did you declare that before using – Tippu Fisal Sheriff Oct 13 '22 at 09:14
11
You should use startActivityForResult
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
See "get contact info from android contact picker" for more information.

Community
- 1
- 1

Kamil Powałowski
- 267
- 1
- 11
-
1+1 for using `ContactsContract.Contacts.CONTENT_URI` because `Contacts.CONTENT_URI` is deprecated. – Darcy Apr 08 '13 at 01:51
1
try this code
Intent intent = new Intent(Intent.ACTION_DEFAULT, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
Use ACTION_DEFAULT
instead of ACTION_PICK
.
Good Luck.

A. Mesgari
- 13
- 4