Get Phone Contacts and display in Listview in Android

Hello Guys !!

In this tutorials, you will learn how to get Phone contact and display in listview in your android application.

Here is the full example to read the phone contact and display in listview.

Here is the java files :

1. GetContactActivity.java

package com.pack.getcontact;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class GetContactActivity extends Activity{
ListView contactLV;
ArrayList<HashMap<String , String>> ContactList = new ArrayList<HashMap<String,String>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
contactLV = (ListView) findViewById(R.id.contactList);
getContactList();
}

public class ContactAdapter extends BaseAdapter {
private     LayoutInflater inflater = null;
private Context context;
ArrayList<HashMap<String, String>> contactlist2;

public ContactAdapter(Context context,
ArrayList<HashMap<String, String>> contactinfo) {

// TODO Auto-generated constructor stub
this.context = context;
this.contactlist2 = new ArrayList<HashMap<String, String>>();
this.contactlist2.addAll(contactinfo);
inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return contactlist2.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public class ViewHolder {
TextView txt_name, txt_number;
}

@Override
public View getView(final int position, View view, ViewGroup parent) {

ViewHolder holder;
holder = new ViewHolder();
if (view == null) {

view = inflater.inflate(R.layout.layout_contact, null);

holder.txt_name = (TextView) view.findViewById(R.id.nameTV);
holder.txt_number = (TextView) view.findViewById(R.id.numberTV);

view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}

holder.txt_name.setText(contactlist2.get(position).get("NAME"));
holder.txt_number.setText(contactlist2.get(position).get("NUMBER"));

return view;
}
}

public void getContactList() {
// TODO Auto-generated method stub
String phoneNum[] = null;
Cursor cur = null;
Cursor pCur = null;
try {
ContentResolver cr = getContentResolver();
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
int i = 0;
if (cur.getCount() > 0) {

phoneNum = new String[cur.getCount()];
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
//String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
// get the phone number
pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[] { id },
"'"+ ContactsContract.Contacts.DISPLAY_NAME+ "' ASC");
while (pCur.moveToNext()) {
//int _id = i;
String _name = pCur.getString(pCur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String _number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
_number = _number.replaceAll("[\\D]", "");

HashMap<String , String> contactMap= new HashMap<String, String>();
contactMap.put("NAME", _name);
contactMap.put("NUMBER", _number);

ContactList.add(contactMap);
phoneNum[i] =  _number;
}
pCur.close();
}
i++;
}
}
cur.close();
} catch ( Exception e ) {
// TODO: handle exception
}
finally {
if(pCur != null){
pCur.close();
}
if(cur != null){
cur.close();
}
}
contactLV.setAdapter(new ContactAdapter(GetContactActivity.this, ContactList));
}
}


Here are the xml files

1. activity_contact.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/contactList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:divider="#ccc"
        android:dividerHeight="1dp" >
    </ListView>

</LinearLayout> 


2. layout_contact.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="#fafafa"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/nameTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="name"
        android:textColor="#000"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/numberTV"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="number"  
        android:textColor="#000"
        android:textStyle="normal" />

</LinearLayout>



One more thing, to read the phone contact you have to add permission in your manifest file to read phone contacts.

 <uses-permission android:name="android.permission.READ_CONTACTS" />


Here is the output of the program :




Happy Coding !!

Comments

Popular posts from this blog

Carousel view in Android

Draw Pie Chart using Canvas and Third party Library in Android