Welcome to get contact list in Android Studio tutorial.
Get contact list in Android Studio example guides you to get a contact list and show in the custom listview.
Get contact list in Android example will show how to show all contacts alphabetically.
If you want to access contact list from Intent, then visit contact list from Intent.
Visit the official link to get contact list.
First, go through the output, then we will develop with Android Studio.
Download Source Code for get contact list in Android Studio
[sociallocker]Download ContactListviewDemonuts [/sociallocker]
Step 1: Create a new project in Android Studio.
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.exampledemo.parsaniahardik.contactlistviewdemonuts"> <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: Adding ListView Item file
Make a new layout resource file named “lv_item.xml”
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#fff" android:textSize="25sp" android:gravity="center_vertical" android:paddingLeft="10dp" android:layout_marginTop="10dp" android:text="Name" /> <TextView android:id="@+id/number" android:text="ds" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#fff" android:textSize="25sp" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="@color/colorAccent"/> </LinearLayout>
Step 4: Creating a model class
Create a new class named “ContactModel.”
public class ContactModel { private String name, number; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } }
Step 5: Making a custom adapter class
Prepare a new class and give a name as “CustomAdapter.”
import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.ArrayList; /** * Created by Parsania Hardik on 11-May-17. */ public class CustomAdapter extends BaseAdapter { private Context context; private ArrayList<ContactModel> contactModelArrayList; public CustomAdapter(Context context, ArrayList<ContactModel> contactModelArrayList) { this.context = context; this.contactModelArrayList = contactModelArrayList; } @Override public int getViewTypeCount() { return getCount(); } @Override public int getItemViewType(int position) { return position; } @Override public int getCount() { return contactModelArrayList.size(); } @Override public Object getItem(int position) { return contactModelArrayList.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.lv_item, null, true); holder.tvname = (TextView) convertView.findViewById(R.id.name); holder.tvnumber = (TextView) convertView.findViewById(R.id.number); convertView.setTag(holder); }else { // the getTag returns the viewHolder object set as a tag to the view holder = (ViewHolder)convertView.getTag(); } holder.tvname.setText(contactModelArrayList.get(position).getName()); holder.tvnumber.setText(contactModelArrayList.get(position).getNumber()); return convertView; } private class ViewHolder { protected TextView tvname, tvnumber; } }
Step 6: Updating MainActivity
Update activity_main.xml as below source code
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.exampledemo.parsaniahardik.contactlistviewdemonuts.MainActivity"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listView" android:divider="@null" /> </RelativeLayout>
Update MainActivity.java class as following
import android.database.Cursor; import android.provider.ContactsContract; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ListView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private ListView listView; private CustomAdapter customAdapter; private ArrayList<ContactModel> contactModelArrayList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView); contactModelArrayList = new ArrayList<>(); Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC"); while (phones.moveToNext()) { String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); ContactModel contactModel = new ContactModel(); contactModel.setName(name); contactModel.setNumber(phoneNumber); contactModelArrayList.add(contactModel); Log.d("name>>",name+" "+phoneNumber); } phones.close(); customAdapter = new CustomAdapter(this,contactModelArrayList); listView.setAdapter(customAdapter); } }
Step 7: Describing MainActivity.java class
Let’s make an eye on below source code
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC"); while (phones.moveToNext()) { String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); ContactModel contactModel = new ContactModel(); contactModel.setName(name); contactModel.setNumber(phoneNumber); contactModelArrayList.add(contactModel); Log.d("name>>",name+" "+phoneNumber); } phones.close();
We are using Cursor class to get details about contacts.
We will store contact’s name and phone number in two different variables.
After that, we will create a object of ContactModel class and will add this object to the contactModelArrayList.
After getting all the contacts information, we will set whole information in ListView.
Kotlin Version
This example is in Kotlin here: Android Get Contact List Kotlin
Ask your any questions regarding this tutorial in comment section.
You can also give your reviews and recommendations in comment section as well.
So the end for get contact list in android studio programmatically tutorial. Thank you.