Profile
December 22, 2022

Pick a Contact from Contact List of Android Phone

To pick a contact from the contact list in Android, you can use the Intent class and the ACTION_PICK action


Step1:

create an Intent to pick a contact from the contact list using the ACTION_PICK action and set the type of CONTENT_TYPE


val intent = Intent(Intent.ACTION_PICK)
intent.type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE

 

Step2:

create an Intent Launcher to launch the implicit Intent of Contact Picker:


var intenttLauncher =
       registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
           if (result.resultCode == Activity.RESULT_OK) {
               result.data?.let {
                        try {
       val uri: Uri? = data.data
       val cols = arrayOf(
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Phone.NUMBER
       )
       cursor =
        uri?.let {
         context.requireActivity().contentResolver.query(it, cols, null, null, null)
        }
       cursor?.moveToFirst()
       val name = cursor?.getString(0)   // here you go
       val phoneNo = cursor?.getString(1)    

      } catch (e: java.lang.Exception) {
       e.printStackTrace()
      }
               }
           }
       }


Step3:

launch the Intent from Activity or Fragment:

intenttLauncher.launch(intent)
Last edited on December 22, 2022 by Admin

Latest articles