Get the Email from Contacts using AutoComplete TextView? - android

In my application i've one AutoComplete TextView and EditText In this AutoComplete TextView provides all the contact names from Contacts.
I want to get the primary email id which contact was selected in my AutoComplete Textview to the editText. How can i achieve this? Anyone guide me?

public class Contact extends Activity {
/** Called when the activity is first created. */
private static final int CONTACT_PICKER_RESULT = 10;
private static final String DEBUG_TAG = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void doLaunchContactPicker(View view) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String email = "";
try {
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: "
+ result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for everything email
cursor = getContentResolver().query(Email.CONTENT_URI, null, Email.CONTACT_ID + "=?", new String[] { id }, null);
int emailIdx = cursor.getColumnIndex(Email.DATA);
// let's just get the first email
if (cursor.moveToFirst()) {
email = cursor.getString(emailIdx);
Log.v(DEBUG_TAG, "Got email: " + email);
} else {
Log.w(DEBUG_TAG, "No results");
}
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to get email data", e);
} finally {
if (cursor != null) {
cursor.close();
}
EditText emailEntry = (EditText) findViewById(R.id.invite_email);
emailEntry.setText(email);
if (email.length() == 0) {
Toast.makeText(this, "No email found for contact.",
Toast.LENGTH_LONG).show();
}
}
break;
}
} else {
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}
}

Make sure you have READ_CONTACTS permission for your App.
AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, null, null, null);
startManagingCursor(emailCursor);
autoCompleteTextView.setAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, emailCursor, new String[] {Email.DATA1}, new int[] {android.R.id.text1}));
autoCompleteTextView.setThreshold(0);
Please Note AutoCompleteTextView is Case Sensitive.

Related

Android query for email with given email id

I let the user choose email from contacts with following intent:
Intent emailPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);
startActivityForResult(emailPickerIntent, Constants.CONTACT_PICKER_RESULT);
How can I query for the email the user chose in onActivityResult method?
Following code will help you to get the email address onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
switch (requestCode)
{
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String email = "", name = "";
try {
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for everything email
cursor = getContentResolver().query(Email.CONTENT_URI, null, Email._ID + "=" + id, null, null);
int nameId = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
int emailIdx = cursor.getColumnIndex(Email.DATA);
// let's just get the first email
if (cursor.moveToFirst()) {
email = cursor.getString(emailIdx);
name = cursor.getString(nameId);
Log.v(DEBUG_TAG, "Got email: " + email);
} else {
Log.w(DEBUG_TAG, "No results");
}
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to get email data", e);
} finally {
if (cursor != null) {
cursor.close();
}
EditText emailEntry = (EditText) findViewById(R.id.editTextv);
EditText personEntry = (EditText) findViewById(R.id.person);
emailEntry.setText(email);
personEntry.setText(name);
if (email.length() == 0 && name.length() == 0)
{
Toast.makeText(this, "No Email for Selected Contact",Toast.LENGTH_LONG).show();
}
}
break;
}
} else {
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}
}
You also have to add following permission in your manifest file:
<uses-permission android:name="android.permission.READ_CONTACTS"/>

Details not coming properly by using Intent?

The details of the contacts not displayed properly by using this code. I am trying to access the Name, Number and Email ID by using the Intent. Name and Number only displayed when I click the button but not the Email.No error in the project.It's working good.My xml file have only one Button.
public class GetDetails extends Activity {
/** Called when the activity is first created. */
private static final int CONTACT_PICKER_RESULT = 1001;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button Btn = (Button)findViewById(R.id.getContacts);
Btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(i, CONTACT_PICKER_RESULT);
}
});
}
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if(resultCode == RESULT_OK) {
switch (reqCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
Cursor emails = null;
String number = "";
String emailID = "";
try {
Uri result = data.getData();
//get the id from the uri
String id = result.getLastPathSegment();
//query
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone._ID + " = ? " , new String[] {id}, null);
int numberIdx = cursor.getColumnIndex(Phone.DATA);
if(cursor.moveToFirst()) {
number = cursor.getString(numberIdx);
} else {
}
emails = getContentResolver().query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
int num = emails.getColumnIndex(Email.DATA);
if(emails.moveToFirst()) {
emailID = emails.getString(num);
} else {
}
} catch (Exception e) {
//failed
} finally {
if (cursor!=null) {
cursor.close();
}
}
}
}
}
}
How to solve this situation ?
Replace this line:
emails = getContentResolver().query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
with this line :
emails = getContentResolver().query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = ?", new String[]{id}, null);

How to Get Contact name, number and emai ID from a list of contacts?

I am new to android ,I need to get the details of my contacts, but the details include only 3
Contact name
contact number and
Email ID
when I press a Button it will show these 3 details of my all contacts
I am using android Eclair version 2.1. Any solution ?
By below code you can do that -
public void doLaunchContactPicker(View view) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK) {
switch (requestCode)
{
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String email = "", name = "";
try {
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: " + result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for everything email
cursor = getContentResolver().query(Email.CONTENT_URI, null, Email.CONTACT_ID + "=?", new String[] { id }, null);
int nameId = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
int emailIdx = cursor.getColumnIndex(Email.DATA);
// let's just get the first email
if (cursor.moveToFirst()) {
email = cursor.getString(emailIdx);
name = cursor.getString(nameId);
Log.v(DEBUG_TAG, "Got email: " + email);
} else {
Log.w(DEBUG_TAG, "No results");
}
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to get email data", e);
} finally {
if (cursor != null) {
cursor.close();
}
EditText emailEntry = (EditText) findViewById(R.id.editTextv);
EditText personEntry = (EditText) findViewById(R.id.person);
emailEntry.setText(email);
personEntry.setText(name);
if (email.length() == 0 && name.length() == 0)
{
Toast.makeText(this, "No Email for Selected Contact",Toast.LENGTH_LONG).show();
}
}
break;
}
} else {
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}
And, also refer these links -
Get Contact Details
How to Read Contacts
Get All Contact Details
Don't forget to add the required permission -
<uses-permission android:name="android.permission.READ_CONTACTS"/>
in your AndroidManifest.xml file. And, Just modify this code with your needs.
You can access addressbook like this;
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null,ContactsContract.Contacts.DISPLAY_NAME);
int kisiSayisi = cur.getCount();
if(kisiSayisi > 0)
{
int KisiIndex = 0;
while(cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(BaseColumns._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.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);
while (pCur.moveToNext())
{
String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
//String phoneType = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String dogruGSM = gsmNoKontrol(phone);
if(dogruGSM.compareTo("0") != 0){
Kisi kisi = new Kisi(KisiIndex, name, dogruGSM, false);
MyList.add(kisi);
KisiIndex ++;
}
}
pCur.close();
}
}
}

Contacts wont show up into EditText

I have an application that is supposed to call the contacts phone number into the designated EditText View. I have the button that calls up the contact picker and it works fine but when I click on it it doesn't add the information that I want into the View.
MY ONCLICKlISTENER
public void pickContact(View view) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, GET_CONTACT);
}
MY STARTACTIVITYFORRESULT
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case GET_CONTACT:
Cursor cursor = null;
String Number = "";
try {
Uri result = data.getData();
Log.v(NOTIFICATION_SERVICE, "Got a contact result: "
+ result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for everything email
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] { id },
null);
int emailIdx = cursor.getColumnIndex(Phone.DATA);
// let's just get the first email
if (cursor.moveToFirst()) {
Number = cursor.getString(emailIdx);
Log.v(NOTIFICATION_SERVICE, "Got number: " + Number);
} else {
Log.w(NOTIFICATION_SERVICE, "No results");
}
} catch (Exception e) {
Log.e(NOTIFICATION_SERVICE, "Failed to get number data", e);
} finally {
if (cursor != null) {
cursor.close();
}
EditText mNumbers = (EditText)findViewById(R.id.editNumber);
mNumbers.setText(Number);
if (Number.length() == 0) {
Toast.makeText(this, "No number found for contact.",
Toast.LENGTH_LONG).show();
}
}
break;
}
} else {
Log.w(NOTIFICATION_SERVICE, "Warning: activity result not ok");
}
}
Thanks for any help.
Make sure you add
<uses-permission android:name="android.permission.READ_CONTACTS" />
to your manifest
hey u have not used any Bundles in your code.
Viz: Bundle extras = data.getExtras
For Complete Reference, Check out the site: http://mobile.tutsplus.com/tutorials/android/android-essentials-using-the-contact-picker/

Using contact picker to get multiple data

I was able to successfully launch the contact picker and then set an editText in my activity to the phone number of the selected contact. What I'm trying to do now is to set two other editTexts to the first name and last name, respectively. I added the other editTexts and tried to retrieve the last and first name.
Here's what I have:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor = null;
String number = "";
String lastName = "";
String firstName = "";
try {
Uri result = data.getData();
Log.v(DEBUG_TAG, "Got a contact result: "
+ result.toString());
// get the contact id from the Uri
String id = result.getLastPathSegment();
// query for phone number
cursor = getContentResolver().query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=?", new String[] { id },
null);
int phoneIdx = cursor.getColumnIndex(Phone.DATA);
int lastNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds
.StructuredName.FAMILY_NAME);
int firstNameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds
.StructuredName.GIVEN_NAME);
// get the phone number
if (cursor.moveToFirst()) {
number = cursor.getString(phoneIdx);
lastName = cursor.getString(lastNameIdx);
firstName = cursor.getString(firstNameIdx);
Log.v(DEBUG_TAG, "Got number " + number);
} else {
Log.w(DEBUG_TAG, "No results");
}
} catch (Exception e) {
Log.e(DEBUG_TAG, "Failed to get phone number data", e);
} finally {
if (cursor != null) {
cursor.close();
}
EditText phoneNumberEditText = (EditText) findViewById(R.id.number);
phoneNumberEditText.setText(number);
EditText lastNameEditText = (EditText)findViewById(R.id.last_name);
lastNameEditText.setText(lastName);
EditText firstNameEditText = (EditText)findViewById(R.id.first_name);
firstNameEditText.setText(firstName);
if (number.length() == 0) {
Toast.makeText(this, "No phone number found for this contact.",
Toast.LENGTH_LONG).show();
}
if(lastName.length()==0) {
Toast.makeText(this, "No last name found for this contact.",
Toast.LENGTH_LONG).show();
}
if(firstName.length()==0) {
Toast.makeText(this, "No first name found for this contact.",
Toast.LENGTH_LONG).show();
}
}
break;
}
} else {
Log.w(DEBUG_TAG, "Warning: activity result not ok");
}
}
I must be missing something basic. Any thoughts?
Try this:
String lastName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME))
If you call a managedQuery it will return the contacts full name and the id but no phone number.
Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
cursor.moveToNext(); String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name=cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

Categories

Resources