How To Get Contact List In Android Programmatically
Last Updated : Mar 11, 2024
IN - Android | Written & Updated By - Dikshita
In this article we will show you the solution of how to get contact list in android programmatically, phone Contacts are an extremely important source of data for everyone and hence accessing phone contacts is the basic feature for many applications like Truecaller to help the users and provide them a better experience in accessing phone contacts.
We always have a default application in our mobile to manage contacts but what will happen if we create our own application that will read all our saved contacts and will show them to the user.
We can use the particular application for backup of our contacts. So, in this tutorial, we are going to learn how to get contact list in android programmatically.
Step By Step Guide On How To Get Contact List In Android Programmatically :-
By Using Cursor Class
A cursor is a class that actually contains the result set for a query that was made against a database in Android.
This class contains an API that provides a feature to read the columns which were returned from the query, as well as helps to repeat over the rows of the result set.
Xml <?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/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> 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); 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}; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, data, to); // Calling setAdaptor() method to set created adapter listView.setAdapter(adapter); listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); } }
- In the first step you need to create new project in Android.
- Now u need to navigate to the app> manifests> AndroidManifest.xml file and kindly add below permission there to read contacts. <uses-permission android:name=”android.permission.READ_CONTACTS”></uses-permission>
- Now it’s the moment to design the basic layout of the application. So, kindly navigate to app > res > layout > activity_main.xml and paste the below-written code in the activity_main.xml file.
- Working with the MainActivity.java file. Now u need to navigate to the app > java > package name > MainActivity.java file. Below is actually the main code for the MainActivity.java file.
Conclusion :-
This is the end of a pretty important tutorial in Android. Above was a basic example of How to get contact list in android with the help of a program/code. Later on, we will study more on Android topics.
I hope this article on how to get contact list in android programmatically helps you and the steps and method mentioned above are easy to follow and implement.