Get Show all phone contacts into ListView in android programmatically

How to read Retrieving contacts from android mobile phone and display them inside ListView on button click.

Contacts is one of the most important feature in every mobile phone whether its android device or not because its help us to store our related persons mobile phone number into device. By default android device has its own contact showing application but some times when android app developer needs so specific functionality in his project then you can easily display all the contact list into app. So here is the complete step by step tutorial for Get Show all phone contacts into ListView in android programmatically.

android-project-download-code-button

How to Get Show all phone contacts into ListView in android programmatically.

Please add READ_CONTACTS permission inside your AndroidManifest.xml file.

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

Code for MainActivity.java file.

package com.android_examples.contactsinlistview_android_examplescom;
import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ListView listView ;
    ArrayList<String> StoreContacts ;
    ArrayAdapter<String> arrayAdapter ;
    Cursor cursor ;
    String name, phonenumber ;
    public  static final int RequestPermissionCode  = 1 ;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        listView = (ListView)findViewById(R.id.listview1);

        button = (Button)findViewById(R.id.button1);

        StoreContacts = new ArrayList<String>();

        EnableRuntimePermission();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                GetContactsIntoArrayList();

                arrayAdapter = new ArrayAdapter<String>(
                        MainActivity.this,
                        R.layout.contact_items_listview,
                        R.id.textView, StoreContacts
                );

                listView.setAdapter(arrayAdapter);


            }
        });

    }

    public void GetContactsIntoArrayList(){

        cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);

        while (cursor.moveToNext()) {

            name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

            phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            StoreContacts.add(name + " "  + ":" + " " + phonenumber);
        }

        cursor.close();

    }

    public void EnableRuntimePermission(){

        if (ActivityCompat.shouldShowRequestPermissionRationale(
                MainActivity.this,
                Manifest.permission.READ_CONTACTS))
        {

            Toast.makeText(MainActivity.this,"CONTACTS permission allows us to Access CONTACTS app", Toast.LENGTH_LONG).show();

        } else {

            ActivityCompat.requestPermissions(MainActivity.this,new String[]{
                    Manifest.permission.READ_CONTACTS}, RequestPermissionCode);

        }
    }

    @Override
    public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {

        switch (RC) {

            case RequestPermissionCode:

                if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) {

                    Toast.makeText(MainActivity.this,"Permission Granted, Now your application can access CONTACTS.", Toast.LENGTH_LONG).show();

                } else {

                    Toast.makeText(MainActivity.this,"Permission Canceled, Now your application cannot access CONTACTS.", Toast.LENGTH_LONG).show();

                }
                break;
        }
    }


}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android_examples.contactsinlistview_android_examplescom.MainActivity"
    android:background="#FFF9C4"
    android:padding="4dp">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:id="@+id/listview1"
        android:layout_below="@+id/button1" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="Click Here to load Contacts" />

</RelativeLayout>

Code for AndroidManifest.xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android_examples.contactsinlistview_android_examplescom">

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <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>

Screenshots:

contacts-manager-1

contacts-manager-2

Get Show all phone contacts into ListView in android programmatically

Click here to download Get Show all phone contacts into ListView in android programmatically project with source code.

20 Comments

  1. in java file, what is ”
    arrayAdapter = new ArrayAdapter(
    MainActivity.this,
    R.layout.contact_items_listview,
    R.id.textView, StoreContacts
    ); ”
    R.layout.contact_items_listview and R.id.textView refer to?

  2. R.layout.contact_items_listview refers to adapter parameter and also the R.id.textView.

  3. Hello,sir.
    Why there are two Error ? can not run.
    can’t find symbol: contact_items_listview , textView

    Thank you for your best examples!

  4. Hello,sir.
    Why there are two Error ? can not run.
    can’t find symbol: contact_items_listview , textView
    and when i solved all then unfortunatlly stop error occure.
    thanx for your best example

  5. can’t find symbol: contact_items_listview
    and when i solved all then unfortunatally stop error occured.
    thanx for your best example

  6. can’t find symbol: contact_items_listview
    and when i solved all then unfortunatally stop error occured.
    thanx for your best example

  7. thanx sir i have been complete my task
    bt now i want contact profile(photo)from perticular mobile.how i do this give any solution.
    thanx again in advance

  8. contact_items_listview file missing here when u download Project then u will get

  9. for all those asking about the two errors just write android.R.layout.simple_list_item_1,
    android.R.id.text1,
    instead of that and it will work fine.

  10. Thank you guys,On reading your comments n reply i had solved the problem.

  11. in the Manifest.permission.READ_CONTACTS error on permssion

  12. thank you so much…
    but how can we copy all selected contact from listview to sqlite databse
    ???

    thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *