How to get access to your contact list in Android

In this android programming source code example, we are going to get access to your contact list in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to get access to your contact list in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".DeviceFeatures.DeviceFeaturesActivity7">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bluapp.androidview2.R;

import java.util.ArrayList;
import java.util.List;

public class DeviceFeaturesActivity7 extends AppCompatActivity {
    private RecyclerView list;
    private adapter recyclerAdapter;
    private Cursor cursor;
    private List<Contact> StoreContacts ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_features7);
        list = (RecyclerView) findViewById(R.id.list);
        StoreContacts = new ArrayList<Contact>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        list.setLayoutManager(layoutManager);
        GetContactsList();
    }

    public void GetContactsList(){
        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            StoreContacts.add(new Contact(name, phonenumber));
        }
        recyclerAdapter = new adapter(DeviceFeaturesActivity7.this, StoreContacts);
        list.setAdapter(recyclerAdapter);
        cursor.close();

    }

    public class Contact{
        private String name;
        private String phonenumber;

        public Contact(String name, String phonenumber){
            this.name = name;
            this.phonenumber = phonenumber;
        }

        public String getName(){
            return name;
        }

        public String getPhonenumber(){
            return phonenumber;
        }
    }

    private class adapter extends RecyclerView.Adapter<adapter.myViewHolder> {
        Context context;
        List<Contact> mData;

        public adapter(Context context, List<Contact> data) {
            this.context = context;
            this.mData = data;
        }

        @NonNull
        @Override
        public adapter.myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(context).inflate(R.layout.devicefeatures_adapter7, parent, false);
            return new myViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull adapter.myViewHolder holder, int position) {
            holder.nameTxt.setText(mData.get(position).getName());
            holder.phonenumberTxt.setText(mData.get(position).getPhonenumber());
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        public class myViewHolder extends RecyclerView.ViewHolder {
            TextView nameTxt;
            TextView phonenumberTxt;

            public myViewHolder(View itemView) {
                super(itemView);
                nameTxt = (TextView) itemView.findViewById(R.id.nameTxt);
                phonenumberTxt = (TextView) itemView.findViewById(R.id.phonenumberTxt);
            }
        }
    }
}

devicefeatures_adapter7.xml

<?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="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/nameTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:text="Name"
        android:textColor="#000000"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/phonenumberTxt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="10dp"
        android:text="phone number"
        android:textColor="#000000"
        android:textSize="18sp" />

</LinearLayout>

AndroidManifest.xml

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

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

Add a Comment