Share this post on:

Here we will see that how we can fetch contact from phone to our Android Application with number in Android Studio. We will set the contact number in TextView.

Android Studio

Step 1: Layout Design

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".ContactsOne"
android:background="@color/grey"
android:orientation="vertical"
android:backgroundTint="@color/pink_1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Contact"
android:textColor="@color/white"
android:textSize="25sp"
android:padding="8dp"
android:textStyle="italic"
android:gravity="center"
android:layout_alignParentTop="true"
android:id="@+id/toptoolbartext"></TextView>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_below="@id/toptoolbartext"
>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">



<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="8dp"
app:cardElevation="25dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
app:cardBackgroundColor="@color/grey">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">
<ImageView
android:layout_width="100dp"
android:layout_height="70dp"
android:src="@drawable/contact_vectoriimage"
android:layout_gravity="center"
app:tint="@color/pink_1">
</ImageView>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@color/white"
android:padding="10dp"
android:layout_weight="3"
android:background="@drawable/input_designsend"
android:id="@+id/textcontact1">
</TextView>

<ImageView
android:id="@+id/contacts1"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:scaleType="fitXY"
android:layout_marginLeft="5dp"
android:src="@drawable/contacts_vector"
app:tint="@color/pink_1"></ImageView>
</LinearLayout>



<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@color/white"
android:padding="10dp"
android:layout_weight="3"
android:background="@drawable/input_designsend"
android:id="@+id/textcontact2">
</TextView>

<ImageView
android:id="@+id/contacts2"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:scaleType="fitXY"
android:layout_marginLeft="5dp"
android:src="@drawable/contacts_vector"
app:tint="@color/pink_1"></ImageView>
</LinearLayout>



<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="@color/white"
android:padding="10dp"
android:layout_weight="3"
android:background="@drawable/input_designsend"
android:id="@+id/textcontact3">
</TextView>

<ImageView
android:id="@+id/contacts3"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.5"
android:scaleType="fitXY"
android:layout_marginLeft="5dp"
android:src="@drawable/contacts_vector"
app:tint="@color/pink_1"></ImageView>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="5dp"
android:layout_marginBottom="20dp"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp">
<Button
android:layout_width="200dp"
android:layout_height="35dp"
android:text="Add Contacts"
android:id="@+id/addcontact"
android:layout_gravity="center"
android:textColor="@color/white"
android:background="@color/pink_1"
android:layout_marginTop="8dp">
</Button>
</LinearLayout>

</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
</LinearLayout>

Step 2: Java Class

package com.raynatechnologiesshooket;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.jaeger.library.StatusBarUtil;
import com.raynatechnologiesshooket.conn.config;
import com.raynatechnologiesshooket.utils.MasterFunction;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class ContactsOne extends AppCompatActivity {

    TextView contactphone1,contactphone2,contactphone3;
    Button addcontact;
    private MasterFunction masterFunction;
    
    private static final int RESULT_OK = -1;
    private static final int RESULT_PICK_CONTACT = 1;
    private static final int RESULT_PICK_CONTACT2 = 2;
    private static final int RESULT_PICK_CONTACT3 = 3;

    ImageView contacts1,contact2,contact3;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts_one);
        StatusBarUtil.setTransparent(this);

        contacts1=findViewById(R.id.contacts1);
        contact2=findViewById(R.id.contacts2);
        contact3=findViewById(R.id.contacts3);
        addcontact=findViewById(R.id.addcontact);
        contactphone1=findViewById(R.id.textcontact1);
        contactphone2=findViewById(R.id.textcontact2);
        contactphone3=findViewById(R.id.textcontact3);


        contacts1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // The below two line is needed to open the contact list of  mobile
                Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult(contactPickerIntent,RESULT_PICK_CONTACT);

            }
        });

        contact2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // The below two line is needed to open the contact list of  mobile
                Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult(contactPickerIntent,RESULT_PICK_CONTACT2);

            }
        });

        contact3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // The below two line is needed to open the contact list of  mobile
                Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
                startActivityForResult(contactPickerIntent,RESULT_PICK_CONTACT3);

            }
        });

        addcontact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences settings = getSharedPreferences(PREFER_NAME, 0);
                String UserMobile = settings.getString("phoneNumber", null);
                String FirstContact=contactphone1.getText().toString();
                String SecondContat=contactphone2.getText().toString();
                String ThirdContact=contactphone3.getText().toString();

                if (TextUtils.isEmpty(FirstContact)) {
                    contactphone1.setError("This can't be Empty");
                    contactphone1.setFocusable(true);
                }
                if (TextUtils.isEmpty(SecondContat)) {
                    contactphone2.setError("This can't be Empty");
                    contactphone2.setFocusable(true);
                }
                if (TextUtils.isEmpty(ThirdContact)) {
                    contactphone3.setError("This can't be Empty");
                    contactphone3.setFocusable(true);
                }
                else
                {

             Toast.makeText(getApplicationContext(), "Contact Added Successfully", Toast.LENGTH_LONG).show();

                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {

                case RESULT_PICK_CONTACT:
                    contactPicked(data);
                    break;
                case RESULT_PICK_CONTACT2:
                    secondcontactPicked(data);
                    break;
                case RESULT_PICK_CONTACT3:
                    thirdcontactPicked(data);
                    break;
            }

        } else {
            Toast.makeText(this, "Failed to pick Contact Number", Toast.LENGTH_SHORT).show();
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

    private void contactPicked(Intent data) {
        Cursor cursor=null;
        try {
            String phone= null;
            Uri uri=data.getData();
            cursor = getContentResolver().query(uri,null,null,null,null);
            cursor.moveToFirst();
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            int phoneIndex=cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            phone = cursor.getString(phoneIndex);
            contactphone1.setText(phone);
//You can set name of contact by just taking name.setText(your text view), I have already taken the name but i did'nt initialized it.
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void secondcontactPicked(Intent data) {
        Cursor cursor=null;
        try {
            String phone= null;
            Uri uri=data.getData();
            cursor = getContentResolver().query(uri,null,null,null,null);
            cursor.moveToFirst();
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            int phoneIndex=cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            phone = cursor.getString(phoneIndex);
            contactphone2.setText(phone);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void thirdcontactPicked(Intent data) {
        Cursor cursor=null;
        try {
            String phone= null;
            Uri uri=data.getData();
            cursor = getContentResolver().query(uri,null,null,null,null);
            cursor.moveToFirst();
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            int phoneIndex=cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            phone = cursor.getString(phoneIndex);
            contactphone3.setText(phone);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

Learn more in Android Studio then visit to our Courses

Share this post on:
Avatar Raiyan

Author: Raiyan

Hi, I guess you're here because you want to know a bit about me. huh? I am not so good on talking about myself but I'll give a short about me.

My Name is Raiyan. I am a Professional Application Developer and a Blogger.
I started this website to Share my Knowledge. Here I provide all my knowledge whatever I earned till now.

Leave a Comment

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