How to Read or Access the Contacts Saved in the Phone in Android App Using Android Studio

How to Read or Access the Contacts Saved in the Phone in Android App Using Android Studio The article is a lively article because it contains interesting information and your favorite.   

How to Read or Access the Contacts Saved in the Phone in Android App Using Android Studio

Read or Access the Contacts Saved in the Phone in Android App Using Android Studio

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");
        }
    }
}
<?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="android.permission.WRITE_CONTACTS"/>
    uses-permission android:name="android.permission.READ_CONTACTS"/>

    application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>
<?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">

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

    <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>

Read Also: Alert Dialog With Buttons Example in Android Studio

Conclusion

I hope you find the article How to Read or Access the Contacts Saved in the Phone in Android App Using Android Studio uses. The reason is that we have told you all the information through this article in a way that you can understand. And if you have any doubts, you can express your doubts through the comment box. We also ask that you help share this article with your friends.

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment