Get Contact List Details Android Programmatically
Hello, all learners. Welcome to get contact list details android studio programmatically.
In this get contact list details Android Studio programmatically tutorial we will learn how to retrieve contact name, phone number, email, etc. information in Android app programmatically.
Contact based application will need to access contact details, this tutorial will guide you for this purpose.
First, check the output of get contact list details Android Studio programmatically then we will implement it.
Download Source Code
[sociallocker] Download GetContactDetailsDemonuts [/sociallocker]
Creating get contact list details android studio programmatically example step by step
Step 1: Create a new project in Android Studio.
Always create new project with Empty activity in android studio.
Step 2: Updating AndroidManifest.xml file
add required permissions between <manifest>….</manifest> tag.
1 |
<uses-permission android:name="android.permission.READ_CONTACTS" /> |
Note: If you are targeting SDK version above 22 (Above Lollipop) then you need to ask a user for granting runtime permissions. Check marshmallow runtime permission for more information.
Final code for AndroidManifest.xml file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.exampledemo.parsaniahardik.getcontactdetailsdemonuts"> <uses-permission android:name="android.permission.READ_CONTACTS" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" 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> |
Step 3: Updating activity_main.xml file
We will make user interface for getting name, phone number and email address.
Three textviews are used for this purpose.
Copy and paste below source code in activity_main.xml file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.exampledemo.parsaniahardik.getcontactdetailsdemonuts.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/tvname" android:textColor="#000" android:text="Name:"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/tvphone" android:textColor="#000" android:text="Phone:"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/tvmail" android:textColor="#000" android:text="Email:"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:id="@+id/btn" android:text="Select contact" /> </LinearLayout> |
Step 4: Preparing MainActivity.java class
Add following source code in MainActivity.java class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.net.Uri; import android.provider.ContactsContract; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private Button btn; private TextView tvname, tvphone,tvmail; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); tvname = (TextView) findViewById(R.id.tvname); tvphone = (TextView) findViewById(R.id.tvphone); tvmail = (TextView) findViewById(R.id.tvmail); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intent, 1); } }); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { Uri contactData = data.getData(); Cursor c = getContentResolver().query(contactData, null, null, null, null); if (c.moveToFirst()) { String phoneNumber="",emailAddress=""; String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID)); //http://stackoverflow.com/questions/866769/how-to-call-android-contacts-list our upvoted answer String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if ( hasPhone.equalsIgnoreCase("1")) hasPhone = "true"; else hasPhone = "false" ; if (Boolean.parseBoolean(hasPhone)) { Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); while (phones.moveToNext()) { phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } phones.close(); } // Find Email Addresses Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null); while (emails.moveToNext()) { emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); } emails.close(); //mainActivity.onBackPressed(); // Toast.makeText(mainactivity, "go go go", Toast.LENGTH_SHORT).show(); tvname.setText("Name: "+name); tvphone.setText("Phone: "+phoneNumber); tvmail.setText("Email: "+emailAddress); Log.d("curs", name + " num" + phoneNumber + " " + "mail" + emailAddress); } c.close(); } } } |
Step 5: Description of MainActivity.java
Following source code will open contact list on button click.
1 2 3 4 5 6 7 |
btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intent, 1); } }); |
After clicking on contact list item, onActivityResult() method is called.
Below line will get name of contact
1 |
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); |
Below source code will check whether contact has a phone number or not.
1 2 3 4 5 6 |
String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if ( hasPhone.equalsIgnoreCase("1")) hasPhone = "true"; else hasPhone = "false" ; |
If contact has phone number, then it will be got by following
1 2 3 4 5 6 7 8 9 |
if (Boolean.parseBoolean(hasPhone)) { Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); while (phones.moveToNext()) { phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); } phones.close(); } |
Following source code will take the Email address.
1 2 3 4 5 6 7 |
// Find Email Addresses Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null); while (emails.moveToNext()) { emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); } emails.close(); |
Then finally, all text are set in respected textviews
1 2 3 |
tvname.setText("Name: "+name); tvphone.setText("Phone: "+phoneNumber); tvmail.setText("Email: "+emailAddress); |
Kotlin Version
Kotlin version of this tutorial is at here: Contact List Details Android Kotlin
So all for get contact list details android studio programmatically example tutorial. Thank you.
hi,
i got problem,
when i open contact list, list is open successfully, but when i press back without click on contact, app got crash
See your logcat, which line is making crash?
Hi, this code was so help full for me, i need some more help from u, i need to select specific number of a person, for eg: i have saved someones name with two or more numbers so how could i select specific number. if u can help me, let me know, thank you.
I also need to dig out about selecting specific number.
I will make separate tutorial on this topic in near future.