Pick Contact - Android Studio - Java
Implement the 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.
Video Tutorial:
Code:
Add permission in AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.blogspot.atifsoftwares.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.technifysoft.myapplication; 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 androidx.activity.EdgeToEdge; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; import androidx.appcompat.app.AppCompatActivity; import androidx.core.content.ContextCompat; import androidx.core.graphics.Insets; import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.technifysoft.myapplication.databinding.ActivityMainBinding; public class MainActivity extends AppCompatActivity { private ActivityMainBinding binding; //UI Views private ImageView thumbnailIv; private TextView contactTv; private FloatingActionButton addContactFab; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); EdgeToEdge.enable(this); binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); ViewCompat.setOnApplyWindowInsetsListener(binding.getRoot(), (v, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); //init UI Views thumbnailIv = findViewById(R.id.thumbnailIv); contactTv = findViewById(R.id.contactTv); addContactFab = findViewById(R.id.addFab); //handle click, to pick contact addContactFab.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 return ContextCompat.checkSelfPermission( this, Manifest.permission.READ_CONTACTS) == (PackageManager.PERMISSION_GRANTED ); //true if permission granted, false if not } private void requestContactPermission() { //permissions to request String permission = Manifest.permission.READ_CONTACTS; requestPermissionLauncher.launch(permission); } private void pickContactIntent() { //intent to pick contact Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); contactPickerLauncher.launch(intent); } private final ActivityResultLauncher<String> requestPermissionLauncher = registerForActivityResult( new ActivityResultContracts.RequestPermission(), isGranted -> { if (isGranted) { // Permission is granted. Continue the action or workflow in your // app. pickContactIntent(); } else { // Explain to the user that the feature is unavailable because the // features requires a permission that the user has denied. Toast.makeText(this, "Permission denied...", Toast.LENGTH_SHORT).show(); } }); private final ActivityResultLauncher<Intent> contactPickerLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK) { //handle intent results Intent data = result.getData(); if (data != null) { //calls when user click a contact from list Uri uri = data.getData(); //get data from intent Cursor 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 contactThumbnail = 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) { Cursor 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 (contactThumbnail != null) { thumbnailIv.setImageURI(Uri.parse(contactThumbnail)); } else { thumbnailIv.setImageResource(R.drawable.ic_person); } } cursor2.close(); } cursor1.close(); } } } else { //calls when user click back button | don't pick contact Toast.makeText(this, "No contact selected...", Toast.LENGTH_SHORT).show(); } }); }



Comments
Post a Comment