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
<? xml version = "1.0" encoding = "utf-8" ?> < RelativeLayout android:id = "@+id/relative_layout" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" > <!--Button to perform clicking event to access contacts list inside listView--> < Button android:id = "@+id/Button" android:layout_width = "150dp" android:layout_height = "52dp" android:layout_centerHorizontal = "true" android:layout_margin = "12dp" android:background = "#0F9D58" android:text = "Click Here" android:textColor = "#FFFFFF" /> <!--ListView to show all saved contacts--> < ListView android:id = "@+id/ListView" android:layout_width = "match_parent" android:layout_height = "match_parent" android:layout_below = "@+id/Button" /> </ RelativeLayout > |
Step 4: Working with the MainActivity.java file
Navigate to the app > java > package name > MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
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.appcompat.app.AppCompatActivity; import androidx.cursoradapter.widget.SimpleCursorAdapter; public class MainActivity extends AppCompatActivity { Cursor cursor; ListView listView; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); // declaring listView using findViewById() listView = findViewById(R.id.ListView); // declaring button using findViewById() button = findViewById(R.id.Button); // set OnClickListener() to the button button.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // calling of getContacts() getContacts(); } }); } public void getContacts() { // create cursor and query the data cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null , null , null , null ); startManagingCursor(cursor); // 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 = {android.R.id.text1, android.R.id.text2}; // 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 listView.setAdapter(adapter); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); } } |
Congratulation, now our application is ready so click on the run button. Here is the output video of the application.