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
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();
}
}
}
}
}