How to get contact number from contactlist in Android? - android

I want to get contact number from contact list. In Android application on button i want get number from contact list of phone.
Means it click on Select button, & open contact list. it select number, & display in textview.
Please give me a solution.
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
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) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact name.
}
}
break;
}
}

I got this answer from the following link
http://tutorials-android.blogspot.in/2011/11/how-to-call-android-contacts-list.html

Related

How do I open a contact selection screen with "search by default"

How can I navigate to this screen using android intent
Add Read Contacts permissions.
Use the code below.
public void showContactsChooser(final View view){
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
We now get a screen that is showing us all the contacts we have. We choose one and we are getting back to our app.
To read this contact I am using this method:
#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 = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()){
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
}
}
}
}
For further info use this link.
https://developer.android.com/training/contacts-provider/retrieve-names

Picking phone or email from contact list in android

In an android project i had this intent to pick a phone number from my contact list
btnContacts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Open Contacts
Intent intent= new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
}
});
and this to get the result
#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 contactUri = data.getData();
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME };
Cursor cursor = getActivity().getContentResolver().query(contactUri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
insertedPhoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
formatContact(insertedPhoneNumber, name);
}
}
break;
}
}
I would like to know if there is a way to do the same operation but if the contact dindt had phone number i would retrive the email instead.
I would like to know if there is a way to do the same operation but if the contact dindt had phone number i would retrive the email instead.
No. You ask for phone numbers you get phone numbers. If you need additional logic or ifs, then you need to code it yourself.

Get phone number from Contacts - Crashes in my onActivityResult method when using ContactsContract

I created an onActivityResult method. I am in the contact list, Upon clicking I should get the name and the phone number of the contact, but it crashes.
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
//TODO Auto-generated method stub
super.onActivityResult(reqCode, resultCode, data);
if (reqCode == PICK_CONTACT) {
if(resultCode == ActionBarActivity.RESULT_OK){
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if(c.moveToFirst()) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String phone = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(this, "You've picked" + name, Toast.LENGTH_SHORT).show();
}
}
}
}
Please note, if I remove the line
String phone = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
it doesn't crash. (But I don't get the phone number.

get 0 when selecting contact from contact list

I have made a application in which i want to user to select the contact from the contact list and display it in the EditText.But when ever my user selects a contact my edit text value shows only 0.Why this so???
Code
case R.id.bt_contacts:
intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
break;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == getActivity().RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (c.moveToFirst()) {
contactNumber = c.getInt(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
etMobile.setText("" + contactNumber);
}
}
I think you should get string value from cursor
contactNumber = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

Contacts from phone in own application

How can I implement contacts in my own app? I want to, when I click on button, show all contacts from my phone. Can you tell me something about it? Like in when you want send SMS. Click on button, choose contacts and send.
First you query the full contacts and add into list view and select contact, then you search of query contacts.
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);
phones.moveToFirst();
for(int i =0;i<phones.getCount();i++)
{
String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
}
you need to starActivityforResult with below intent
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
define a constant like int PICK_CONTACT = 1;
startActivityForResult(intent, PICK_CONTACT);
and should have onActivityResult method such as below.
#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 Whatever you want to do with the selected contact name.
}
}
break;
}
}
here is complete tutorial for this how-to-call-android-contacts-list

Categories

Resources