How do I view contact list and Pick contact on Android?

Codeplayon
2 min readDec 10, 2022

--

How do I view contact list and Pick contact on Android?

Hi, developer in this Android tutorial we are learning about the Pick contact number. Like you create an Application form in your form there are so many files like Name, Email, Address, Mobile number, etc. And also feed like reference number and you can use it to file this number from the mobile content list.

So In this flow, you need to add an image, and when the user clicks on this image opens a mobile content list and when you select a number the number is set on your Reference Number field.

To have the user select a contact and provide your app access to all the contact information, we can use the ACTION_PICK action and pass ContactsContract.Contacts.CONTENT_URI to the Intent which will be used to create the app. This is the Intent constructor –

Pick contact on Android Souce code

Follow below easy steps to make it in your App.

Pick contact number Solution 1:-

1. Add Permission in your application manifest file.

<uses-permission android:name="android.permission.READ_CONTACTS"/>

2. Add the Click listener method on the image and use these codes.

static final int PICK_CONTACT=1;
Pick_Contact_Number.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, PICK_CONTACT);
}
});

3. Add a Pick contact list onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
   if(requestCode == PICK_CONTACT){
if (resultCode == Activity.RESULT_OK) {
Cursor cursor = null;
try {
String phoneNo = null ;
String name = null;
Uri uri = data.getData();
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
phoneNo = cursor.getString(phoneIndex);
phoneNumberEditText.setText(phoneNo);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

Pick contact Solution 2:- FOCUS EVENT OF EDIT TEXT:

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

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

you can use both on the solution as per request one is on click listener and get the content list in android and the other is on FocusChangeListener on edit test and get a content list.

Read More:-

--

--

Codeplayon
Codeplayon

Written by Codeplayon

Codeplayon - Mobile App Development & SEO Digital Marketing Company , Gurugram & Delhi, visit our website :-https://www.codeplayon.com/ Mobile No:- 9671718285

No responses yet