Skip to content
Related Articles
Open in App
Not now

Related Articles

How to Get Saved Contacts in Android Application using Cursor?

Improve Article
Save Article
Like Article
  • Difficulty Level : Expert
  • Last Updated : 14 Feb, 2021
Improve Article
Save Article
Like Article

Phone Contacts are very important source of data for everyone and hence accessing phone contacts is the main feature for many applications like Truecaller to help their users and provide them a better experience. We all have a default application in our mobile to manage contacts but what happens if we will create our own application that will read all the saved contacts and will show them to the user. We can use that application for backup of our contacts. So, in this article, we are going to learn how to develop an application that will read contacts.

Note: A Cursor is a class that contains the result set for a particular query that was made against a database in Android. This class has an API that provides a facility to read the columns which were returned from the query, as well as helps to iterate over the rows of the result set.

What we are going to build in this article? 

We will be building a simple application in which we will be displaying a Button and when we will click on that button, and it will add all saved contacts (their names and numbers) into ListView of our application. Note that we have to give permission to our application to read our phone contacts. We will use Java language for developing this project.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add Permission 

Now, navigate to the app> manifests> AndroidManifest.xml file and add below permission there to read contacts.

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

Step 3: Working with the activity_main.xml file

Now it’s time to design the layout of the application. So, navigate to app > res > layout > activity_main.xml and paste the below-written code in the activity_main.xml file. 


<?xml version="1.0" encoding="utf-8"?>
    <!--Button to perform clicking event to 
        access contacts list inside listView-->
        android:text="Click Here"
        android:textColor="#FFFFFF" />
    <!--ListView to show all saved contacts-->
        android:layout_below="@+id/Button" />

Step 4: Working with the file

Navigate to the app > java > package name > file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import androidx.cursoradapter.widget.SimpleCursorAdapter;
public class MainActivity extends AppCompatActivity {
    Cursor cursor;
    ListView listView;
    Button button;
    protected void onCreate(Bundle savedInstanceState) {
        // declaring listView using findViewById()
        listView = findViewById(;
        // declaring button using findViewById()
        button = findViewById(;
        // set OnClickListener() to the button
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // calling of getContacts()
    public void getContacts() {
        // create cursor and query the data
        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        // data is a array of String type which is
        // used to store Number ,Names and id.
        String[] data = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone._ID};
        int[] to = {,};
        // creation of adapter using SimpleCursorAdapter class
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, data, to);
        // Calling setAdaptor() method to set created adapter

Congratulation, now our application is ready so click on the run button. Here is the output video of the application.


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!