contact list in android

How Retrieve contact list in android

How Retrieve contact list in android

We know that there has an inbuilt contact list or phone book in our phone. But if we want that in our application or want a custom phonebook in our device, we need to read this tutorial. We take an Activity class (ContactListActivity.java) , one adapter class (ContactAdapter.java) for showing the list and one data collection class(ContactCollection.java). You can use the shortcode below but before use the code, need to add permission of ‘READ_CONTACTS’ in application manifest file.

Learn how to add permission in android manifest file

Permission :-

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

Short code:-

public void getMyContacts() {

              Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

              if (cursor.getCount() > 0) {

                  while (cursor.moveToNext()) {

                         String profile_image_url="";

                        String contact_id ="";

                        String contact_person ="";

                        String contact_state="";

                  contact_id= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

                  contact_person=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                  profile_image_url = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));

                  contact_state= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                  if (contact_state!=null) {

                     if (!contact_state.equalsIgnoreCase("")) {

                           String contact_no=getSameIdMultipleContactNo(contact_id);

                           String mail_id= getSameIdMultipleMailId(contact_id);

                            if (profile_image_url!=null) {

                //add to your collection class

                           }else {

 //add to your collection class with default image

                            }

                     }     

                     }

                 }

              }

              }

 

 public String getSameIdMultipleContactNo( String contact_id){

              String result="";

              Cursor cursor_pnone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?", new String[] { contact_id }, null);

            while (cursor_pnone.moveToNext()) {

            String phone_no = cursor_pnone.getString(cursor_pnone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

  result=""+phone_no+"\n"+result;

             }

            cursor_pnone.close();

              return result;

       }

 public String getSameIdMultipleMailId(String contact_id){

              String result="";

       Cursor mail_cursor= getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = ?", new String[] { contact_id }, null);

            while (mail_cursor.moveToNext()) {

           String email_id = mail_cursor.getString(mail_cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

           String email_type = mail_cursor.getString(mail_cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

           String email_description =email_id+"("+email_type+")";

           result=""+email_description+"\n"+result;

             }

             mail_cursor.close();

              return result;

       }

Check Source Code.


ContactListActivity.java


package com.eandroidfromhome.contactlist;

import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ListView;

public class ContactListActivity extends Activity {

    

    private ListView lv_contact;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_list);
        
        
        
        lv_contact=(ListView)findViewById(R.id.lv_contact);    
        
        new ContactTask().execute("");
            }

        
    
    public class ContactTask extends AsyncTask<String, Void, Void> {
        
        private ProgressDialog progDailog;
        
        protected void onPreExecute() {
            
            ContactCollection.contactCollectionArr=new ArrayList<ContactCollection>();
            
            progDailog=new ProgressDialog(ContactListActivity.this);        
            progDailog.setCancelable(false);
            progDailog.setMessage("Please Wait...");
            progDailog.show();    

        }

        @Override
        protected Void doInBackground(String... arg0) {
            
            getMyContacts();
            
            return null;
            
            
            }
        

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
            
        ContactAdapter ca=new ContactAdapter(ContactListActivity.this,
                R.layout.list_item, ContactCollection.contactCollectionArr);
        
        lv_contact.setAdapter(ca);
    
        progDailog.dismiss();
                
        
    }}
    
    @SuppressLint("InlinedApi")
    public void getMyContacts() {
        
        Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
          
    
          
          if (cursor.getCount() > 0) {
              
           while (cursor.moveToNext()) {
               
               String profile_image_url="";
               String contact_id ="";
               String contact_person ="";
               String contact_state="";
               
            contact_id= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            
            contact_person=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            profile_image_url = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
            
            contact_state=  cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
        
            
            if (contact_state!=null) {
            if (!contact_state.equalsIgnoreCase("")) {
                 String contact_no=getSameIdMultipleContactNo(contact_id);

                  String mail_id= getSameIdMultipleMailId(contact_id);
                  
                  
                  if (profile_image_url!=null) {
                      ContactCollection.contactCollectionArr.add(new
                     ContactCollection(contact_no, mail_id, profile_image_url, contact_person));
                      
                      
                }else {
                      ContactCollection.contactCollectionArr.add(new
                     ContactCollection(contact_no, mail_id, "", contact_person));
                                    
                }
                    
            }    
            }
           }

          }
         }
    
    
    
    
    public String getSameIdMultipleContactNo( String contact_id){
        String result="";
         Cursor cursor_pnone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?", new String[] { contact_id }, null);
         while (cursor_pnone.moveToNext()) {
         String  phone_no = cursor_pnone.getString(cursor_pnone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
         
         result=""+phone_no+"\n"+result;
         
         }
         cursor_pnone.close();
        return result;
    }
    
    
    public String getSameIdMultipleMailId(String contact_id){
        String result="";
        
         Cursor mail_cursor= getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID+ " = ?", new String[] { contact_id }, null);
            
         while (mail_cursor.moveToNext()) {
            
        String  email_id = mail_cursor.getString(mail_cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
          
        String  email_type = mail_cursor.getString(mail_cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
       
          
        String  email_description =email_id+"("+email_type+")";
       
        result=""+email_description+"\n"+result;
           
         }

         mail_cursor.close();
        return result;
    }

}


ContactCollection.java


 

package com.eandroidfromhome.contactlist;

import java.util.ArrayList;

public class ContactCollection {

    public static ArrayList<ContactCollection> contactCollectionArr=null;

    public String phone_no="";
    public String mail_id="";
    public String image_url="";
    public String name="";
    
    public ContactCollection(String phone_no,String mail_id,String image_url,String name){
        this.phone_no=phone_no;
        this.mail_id=mail_id;
        this.image_url=image_url;
        this.name=name;
        
    }
    
}

 


 

 

ContactAdapter.java


 

package com.eandroidfromhome.contactlist;

import java.util.ArrayList;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ContactAdapter extends ArrayAdapter<ContactCollection>{
    
    private final Context context;
    private final ArrayList<ContactCollection> values;
    private ViewHolder viewHolder;
    private final int resourceId;

    public ContactAdapter(Context context, int resourceId,ArrayList<ContactCollection> values) {
        super(context, resourceId, values);
        // TODO Auto-generated constructor stub
        
        this.context = context;
        this.values = values;
        this.resourceId = resourceId;
    }
    
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(resourceId, parent, false);
            
                
            viewHolder = new ViewHolder();
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_email = (TextView) convertView.findViewById(R.id.tv_mail);
            viewHolder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
            viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
            
            
                convertView.setTag(viewHolder);
            
            
        }else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ContactCollection list_obj=values.get(position);
        viewHolder.tv_name.setText(list_obj.name);
        viewHolder.tv_phone.setText(list_obj.phone_no);
        viewHolder.tv_email.setText(list_obj.mail_id);
            
        if (!list_obj.image_url.equalsIgnoreCase("")) {
            Uri uri=Uri.parse(list_obj.image_url);    
            viewHolder.iv_image.setImageURI(uri);
            }else{
                
                //viewHolder.iv_image.setImageURI(null);
                viewHolder.iv_image.setImageResource(R.drawable.kitkat);
                }
            
        return convertView;
    }
    
    
    public class ViewHolder {

         TextView tv_name;
         TextView tv_phone;
         TextView tv_email;
         ImageView iv_image;
        
    }

}

 

 


 

 

Source code of xml layout.

 

activity_contact_list.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:orientation="vertical" >

        <ListView
            android:id="@+id/lv_contact"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>

    </LinearLayout>

</LinearLayout>


list_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="wrap_content"
    android:layout_gravity="center"
    android:background="#ffffff"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:gravity="center"
        android:padding="6dp" >

        <FrameLayout
            android:layout_width="62dp"
            android:layout_height="62dp"
            android:layout_margin="2dp" >

            <ImageView
                android:id="@+id/iv_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:padding="3dp" />

        </FrameLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="2dp"
                android:text="TextView"
                android:textColor="#000000"
                android:textSize="16dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tv_phone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLength="40"
                android:padding="2dp"
                android:text="Some Text Goes here.."
                android:textColor="#343434"
                android:textSize="12dp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/tv_mail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLength="40"
                android:padding="2dp"
                android:text="Some Text Goes here.."
                android:textColor="#343434"
                android:textSize="12dp"
                android:textStyle="bold" />

        </LinearLayout>

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/arrow_list" />

    </LinearLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#dfdfdf" />

</LinearLayout>

 


 

Download Full Source Code

 

 

Related Post:-

Image Download from server to android

Use Activity as dialog in android

How use html page in android

Splash Screen example in android

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget