Hello guys !!
If you want to fetch contact list from your android device then you are at right place. Contact book read using ContentResolver class.
First you need to make ContentResolver Object.
ContentResolver cr = getContentResolver();
getContentResolver() return the device Resource access.
Then after make cursor for getting contacts from device resource using ContentResolver object.
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
Now getting name and number from cursor object to your ArrayList<> or array[].
public ArrayList<String> nameArr = new ArrayList<String>(); public ArrayList<String> numArr = new ArrayList<String>();
if (cur.getCount() > 0) { while (cur.moveToNext()) { String id = cur.getString( cur.getColumnIndex(ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) { Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); while (pCur.moveToNext()) { String phoneNo = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); numArr.add(phoneNo + ""); nameArr.add(name + ""); } pCur.close(); } } }
In previous example ChangeWallpaper programatically we discuss about permission so we need take permission in our AndroidMenifest.xml file.
<uses-permission android:name="android.permission.READ_CONTACTS" />
Full Source Code :
activity_contact_read.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:orientation="vertical"> <ListView android:id="@+id/lvContact" android:layout_width="match_parent" android:layout_height="match_parent"></ListView> </LinearLayout>
ContactReadActivity.java
public class ContactReadActivity extends AppCompatActivity { public ListView lvContact; public ArrayList<String> nameArr = new ArrayList<String>(); public ArrayList<String> numArr = new ArrayList<String>(); public ContactAdapter cAdapter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact_read); lvContact = (ListView) findViewById(R.id.lvContact); cAdapter = new ContactAdapter(ContactReadActivity.this, nameArr, numArr); lvContact.setAdapter(cAdapter); ContentResolver cr = getContentResolver(); Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cur.getCount() > 0) { while (cur.moveToNext()) { String id = cur.getString( cur.getColumnIndex(ContactsContract.Contacts._ID)); String name = cur.getString(cur.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME)); if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) { Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); while (pCur.moveToNext()) { String phoneNo = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); numArr.add(phoneNo + ""); nameArr.add(name + ""); } pCur.close(); cAdapter.notifyDataSetChanged(); } } } } }
ContactAdapter.java
public class ContactAdapter extends BaseAdapter { public Context ct; public ArrayList<String> nameArr = new ArrayList<String>(); public ArrayList<String> numArr = new ArrayList<String>(); public ContactAdapter(Context ct, ArrayList<String> nameArr, ArrayList<String> numArr) { this.ct = ct; this.nameArr = nameArr; this.numArr = numArr; } @Override public int getCount() { return nameArr.size(); } @Override public Object getItem(int position) { return nameArr.get(position); } @Override public long getItemId(int position) { return 0; } Holder holder; @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(ct).inflate(R.layout.cutsom_contact_row, null); holder = new Holder(); holder.tvName = (TextView) convertView.findViewById(R.id.tvName); holder.tvNum = (TextView) convertView.findViewById(R.id.tvNum); convertView.setTag(holder); }else{ holder=(Holder)convertView.getTag(); } holder.tvName.setText(nameArr.get(position)); holder.tvNum.setText(numArr.get(position)); return convertView; } class Holder { TextView tvName, tvNum; } }
custom_contact_row.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:orientation="vertical"> <TextView android:id="@+id/tvName" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18dp" android:padding="5dp" android:singleLine="true"/> <TextView android:id="@+id/tvNum" android:textColor="@android:color/black" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18dp" android:padding="5dp" android:singleLine="true"/> </LinearLayout>
Thank you. 🙂
If you have any query or face any problem in your android programming feel free to write here 🙂