Implement Contact Pick feature in and Android App
To allow the user to pick a contact from the phone's contacts list, we can use contact intent. You will get all information of the contact e.g. Name, Phone Number(s), Address, Thumbnail, Email, etc. You can use get the specific information according to your requirement. To pick a contact we need to READ_CONTACTS permission.
Check for Kotlin
Add permission in AndroidMenifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wongngaur.blogspot.pickcontact">
<!--Read contact permission-->
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PickContact">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity">
<!--ImageView: Show contact thumbnail-->
<ImageView
android:id="@+id/thumbnailIv"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@drawable/ic_person"
android:layout_centerHorizontal="true"/>
<!--TextView: Show contact info-->
<TextView
android:id="@+id/contactTv"
android:layout_below="@id/thumbnailIv"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!--FloatingActionButton: Click to pick contact-->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/addFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:src="@drawable/ic_person_add"
android:layout_alignParentEnd="true"
android:layout_margin="10dp"/>
</RelativeLayout>
MainActivity.java
package com.blogspot.wongngaur.pickcontact;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class MainActivity extends AppCompatActivity {
//UI Views
private ImageView thumbnailIv;
private TextView contactTv;
private FloatingActionButton addFab;
private static final int CONTACT_PERMISSION_CODE = 1;
private static final int CONTACT_PICK_CODE = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//init UI Views
thumbnailIv = findViewById(R.id.thumbnailIv);
contactTv = findViewById(R.id.contactTv);
addFab = findViewById(R.id.addFab);
//handle click, to pick contact
addFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//first we need to check read contact permission
if (checkContactPermission()){
//permission granted, pick contact
pickContactIntent();
}
else {
//permission not granted, request
requestContactPermission();
}
}
});
}
private boolean checkContactPermission(){
//check if contact permission was granted or not
boolean result = ContextCompat.checkSelfPermission(
this,
Manifest.permission.READ_CONTACTS) == (PackageManager.PERMISSION_GRANTED
);
return result; //true if permission granted, false if not
}
private void requestContactPermission(){
//permissions to request
String[] permission = {Manifest.permission.READ_CONTACTS};
ActivityCompat.requestPermissions(this, permission, CONTACT_PERMISSION_CODE);
}
private void pickContactIntent(){
//intent to pick contact
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, CONTACT_PICK_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//handle permission request result
if (requestCode == CONTACT_PERMISSION_CODE){
if (grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//permission granted, can pick contact now
pickContactIntent();
}
else {
//permission denied
Toast.makeText(this, "Permission denied...", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//handle intent results
if (resultCode == RESULT_OK){
//calls when user click a contact from list
if (requestCode == CONTACT_PICK_CODE){
contactTv.setText("");
Cursor cursor1, cursor2;
//get data from intent
Uri uri = data.getData();
cursor1 = getContentResolver().query(uri, null, null, null, null);
if (cursor1.moveToFirst()){
//get contact details
String contactId = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts._ID));
String contactName = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String contactThumnail = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
String idResults = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
int idResultHold = Integer.parseInt(idResults);
contactTv.append("ID: "+contactId);
contactTv.append("\nName: "+contactName);
if (idResultHold == 1){
cursor2 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+contactId,
null,
null
);
//a contact may have multiple phone numbers
while (cursor2.moveToNext()){
//get phone number
String contactNumber = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//set details
contactTv.append("\nPhone: "+contactNumber);
//before setting image, check if have or not
if (contactThumnail != null){
thumbnailIv.setImageURI(Uri.parse(contactThumnail));
}
else {
thumbnailIv.setImageResource(R.drawable.ic_person);
}
}
cursor2.close();
}
cursor1.close();
}
}
}
else {
//calls when user click back button | don't pick contact
}
}
}
That's it, tun project and check, here are some screenshots...