How to read or access the contacts saved in the phone from your Android App?

This video shows the steps to develop an Android App to read or access the contact details and information of the numbers stored in the phone’s database.

This App considers both the use case of accessing the contact details either with Number or Name.

If the Number is entered or provided then the respective Name is returned and printed in the edit text for display.

Similarly, if the Name is entered or provided then the respective Phone number is retrieved and displayed on the respective edit Text.

This concept can be used in a larger App, which needs the access to the address book stored in the phone to read and write the contact details.

I hope this video is useful to you. For any suggestions, queries or appreciation, please email us at: programmerworld1990@gmail.com

Source code:

Java code

package com.example.mycontactaccessapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    private EditText editText1;
    private EditText editText2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_CONTACTS}, PackageManager.PERMISSION_GRANTED);

        editText1 = findViewById(R.id.editText);
        editText2 = findViewById(R.id.editText2);
    }

    public void getNameButton(View view){
        try {
            Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(editText2.getText().toString()));
            Cursor cursor = getContentResolver().query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);

            String stringContactName = "INVALID";
            if (cursor != null){
                if (cursor.moveToFirst()){
                    stringContactName = cursor.getString(0);
                }
            }
            editText1.setText(stringContactName);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    public void getNumberButton(View view){
        try {
            Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                    new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE},
                    "DISPLAY_NAME = '" + editText1.getText().toString() + "'", null, null);

            cursor.moveToFirst();
            editText2.setText(cursor.getString(0));
        }
        catch (Exception e){
            e.printStackTrace();
            editText2.setText("NA");
        }
    }
}

Manifest File:

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

    uses-permission android:name=&quot;android.permission.WRITE_CONTACTS&quot;/&gt;
    uses-permission android:name=&quot;android.permission.READ_CONTACTS&quot;/&gt;

    application
        android:allowBackup=&quot;true&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;
        activity android:name=&quot;.MainActivity&quot;&gt;
            intent-filter&gt;
                action android:name=&quot;android.intent.action.MAIN&quot;

                category android:name=&quot;android.intent.category.LAUNCHER&quot;
            /intent-filter;
        /activity;
    /application

</manifest>

Layout XML file:

<?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=".MainActivity">

    &nbsplt;EditText
        android:id=&quot;@+id/editText&quot;
        android:layout_width=&quot;152dp&quot;
        android:layout_height=&quot;46dp&quot;
        android:layout_marginStart=&quot;29dp&quot;
        android:layout_marginTop=&quot;102dp&quot;
        android:ems=&quot;10&quot;
        android:hint=&quot;@string/contact_name&quot;
        android:inputType=&quot;textPersonName&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;
        android:autofillHints=&quot;&quot; /&gt;

    <EditText
        android:id="@+id/editText2"
        android:layout_width="146dp"
        android:layout_height="38dp"
        android:layout_marginStart="54dp"
        android:layout_marginTop="103dp"
        android:ems="10"
        android:hint="@string/contact_number"
        android:inputType="textPersonName"
        app:layout_constraintStart_toEndOf="@+id/editText"
        app:layout_constraintTop_toTopOf="parent"
        android:autofillHints="" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="52dp"
        android:layout_marginTop="60dp"
        android:onClick="getNameButton"
        android:text="@string/get_name"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/editText" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="106dp"
        android:layout_marginTop="64dp"
        android:onClick="getNumberButton"
        android:text="@string/get_number"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toBottomOf="@+id/editText2" />
</androidx.constraintlayout.widget.ConstraintLayout>

Leave a Reply

%d bloggers like this: