Android - Showing Phonebook contacts and selecting one

For Kotlin, the request code for fetching a contact

private val REQUEST_CONTACT = 201

Intent to launch the phone book

private fun fetchPhoneNo() {
    val intent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
    startActivityForResult(intent, REQUEST_CONTACT)
}

Get the phone number in onActivityResult

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (requestCode == REQUEST_CONTACT && data?.data != null) {
        val contactUri = data.data;
        val crContacts = contentResolver.query(contactUri, null, null, null, null);
        crContacts.moveToFirst()
        val id = crContacts.getString(crContacts.getColumnIndex(ContactsContract.Contacts._ID));
        if (Integer.parseInt(crContacts.getString(crContacts.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
            val crPhones =
               contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                     null,
                                     ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                       + " = ?",
                                     arrayOf(id),
                                     null)
            crPhones.moveToFirst()
            var phoneNo = crPhones.getString(
                crPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
            crPhones.close()
        }
        crContacts.close()
    }
}

Intent i=new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);

startActivityForResult(i, PICK_REQUEST);

The Intent delivered to your onActivityResult() method will contain the Uri of the chosen contact -- you will get this by calling getData() on that Intent.

Here is a sample project that demonstrates this, with the logic being implemented in a retained fragment, so we hang onto the selected contact across configuration changes (e.g., user rotating the screen).

You can also use ACTION_GET_CONTENT for this, and I think that's the more modern pattern, though ACTION_PICK certainly works and is all I have sample code for at the time of this writing. If you are reading this in the future (hi, future!), it's possible that the linked-to sample has been updated to use ACTION_GET_CONTENT.


TRY THIS-->

   setContentView(R.layout.main);

   contactNumber = (TextView)findViewById(R.id.contactnumber);

   Button buttonPickContact = (Button)findViewById(R.id.pickcontact);
   buttonPickContact.setOnClickListener(new Button.OnClickListener(){

   @Override
    public void onClick(View arg0) {
   // TODO Auto-generated method stub


   Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
   intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
   startActivityForResult(intent, 1);             


    }});
   }

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

   if(requestCode == RQS_PICK_CONTACT){
   if(resultCode == RESULT_OK){
    Uri contactData = data.getData();
    Cursor cursor =  managedQuery(contactData, null, null, null, null);
    cursor.moveToFirst();

      String number =       cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

      //contactName.setText(name);
      contactNumber.setText(number);
      //contactEmail.setText(email);
     }
     }
     }
     }

EDIT XML ADDED;

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical" >

      <Button
        android:id="@+id/pickcontact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Pick Contact" />

      <TextView
       android:id="@+id/contactnumber"
       android:layout_width="match_parent"
       android:layout_height="wrap_content" />

 </LinearLayout>

I was doing the same thing in my application, and this is how to start a new intent for showing the list

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);

And this is how to process the result:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_CONTACT) {
        if (resultCode == RESULT_OK) {
            Uri contactData = data.getData();
            String number = "";
            Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
            cursor.moveToFirst();
            String hasPhone = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
            String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
            if (hasPhone.equals("1")) {
                Cursor phones = getContentResolver().query
                        (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                        + " = " + contactId, null, null);
                while (phones.moveToNext()) {
                    number = phones.getString(phones.getColumnIndex
                    (ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll("[-() ]", "");
                }
                phones.close();

            // Do something with the number
            }
            else {
                Toast.makeText(getApplicationContext(), "This contact has no phone number", Toast.LENGTH_LONG).show();
            }
            cursor.close();
            }
        }
    }
}

PICK_CONTACT is a constant defined in the class.