Hello, Welcome to get contact list details android kotlin programmatically example.
In this get contact list details Android Kotlin programmatically tutorial we will learn how to retrieve contact name, phone number, email, etc. information in Android app programmatically.
We will learn how to select specific contact with it’s information and we will store them in different variables.
Storing in specific variable will allow us to share these information throughout the whole application.
First, check the output of get contact list details Android Kotlin programmatically then we will implement it.
Download Source Code for get contact list details android kotlin
[sociallocker]Kotlin_Contact_Demo[/sociallocker]
Step 1: Create a new project in Android Studio.
When you are creating new project, select empty activity as default activity.
Step 2: Updating AndroidManifest.xml file
add required permissions between <manifest>….</manifest> tag.
<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
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.parsaniahardik.kotlin_contact_demo"> <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>
Step 3: Updating activity_main.xml file
Copy and paste below source code in activity_main.xml file
<?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="10dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="10dp" tools:context="com.example.parsaniahardik.kotlin_contact_demo.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.kt class
Add following source code in MainActivity.kt class
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 class MainActivity : AppCompatActivity() { private var btn: Button? = null private var tvname: TextView? = null private var tvphone: TextView? = null private var tvmail: TextView? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) btn = findViewById(R.id.btn) as Button tvname = findViewById(R.id.tvname) as TextView tvphone = findViewById(R.id.tvphone) as TextView tvmail = findViewById(R.id.tvmail) as TextView btn!!.setOnClickListener { val intent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI) startActivityForResult(intent, 1) } } public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) { if (resultCode == Activity.RESULT_OK) { val contactData = data.data val c = contentResolver.query(contactData!!, null, null, null, null) if (c!!.moveToFirst()) { var phoneNumber = "" var emailAddress = "" val name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)) val contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID)) //http://stackoverflow.com/questions/866769/how-to-call-android-contacts-list our upvoted answer var hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) if (hasPhone.equals("1", ignoreCase = true)) hasPhone = "true" else hasPhone = "false" if (java.lang.Boolean.parseBoolean(hasPhone)) { val phones = contentResolver.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 val emails = contentResolver.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!!.text = "Name: " + name tvphone!!.text = "Phone: " + phoneNumber tvmail!!.text = "Email: " + emailAddress Log.d("curs", "$name num$phoneNumber mail$emailAddress") } c.close() } } }
Step 5: Description of MainActivity.kt
Following source code will open contact list on button click.
btn!!.setOnClickListener { val intent = 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
val name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
Below source code will check whether contact has a phone number or not.
var hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) if (hasPhone.equals("1", ignoreCase = true)) hasPhone = "true" else hasPhone = "false"
If contact has phone number, then it will be got by following
if (java.lang.Boolean.parseBoolean(hasPhone)) { val phones = contentResolver.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 will take the Email address.
// Find Email Addresses val emails = contentResolver.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
tvname!!.text = "Name: " + name tvphone!!.text = "Phone: " + phoneNumber tvmail!!.text = "Email: " + emailAddress
Java Version
JAVA Version of this tutorial is at here: Get Contact List Details Android Studio Programmatically.
So all for get contact list details android kotlin programmatically example tutorial.
Consider sharing our tutorials in your social media to help us and other learners to grow.
Thank you.