In my app I want to access contacts from the phone and fill the form with some data getting from the contact. When I am trying to run the Intent, I can see the list of contacts in my phone but it doesn't send any data back and onActivityResult is not being called. Can someone please help me as I am a beginner in android programming.
Here is part of my code :
On click event of a button called 'Contacts'
btn_contacts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
@SuppressWarnings("deprecation")
Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
}
});
In onActivityResult in the same activity.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
Log.e("REcAddress","in activity result");
super.onActivityResult(requestCode, resultCode, data);
if(data!=null){
Log.e("REcAddress","hey");
}
if(requestCode == PICK_CONTACT && resultCode == RESULT_OK){
Uri contactData = data.getData();
Log.e("REcAddress",contactData.toString());
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if(c.moveToFirst()){
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.e("REcAddress",name);
edt_rec_name.setText(name);
}
}
}
I have also attached an image to show the layout of the activity. I want to fill in as many details I can from contacts in the form. There is no error in Logcat too. Please, guide me.
Thanks.