Android showing contacts list

In this Android tip, I am going to show you how to read contacts list stored in Android phones. Android stores contact names in ContactsContract.Contacts.CONTENT_URI table and phone numbers in the ContactsContract.CommonDataKinds.Phone.CONTENT_URI. They are related via Contacts._ID field. The relationship of the two tables is one-to-many. This means that one contact is associated to multiple phone numbers.

You can read all contact names and phone numbers from the tables by using the query method of the ContentResolver instance as you did in the Display bookmarks and History of Browser post. Two Cursor objects are needed. One is to store data from the ContactsContract.Contacts.CONTENT_URI table and another one is for storing data from the the ContactsContract.CommonDataKinds.Phone.CONTENT_URI table.


Now to create an application to view contacts list, you need to create a new Android project. Then modify the activity_main.xml file to add a ListView widget as shown below.

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ListView

android:id="@+id/contact_list"
android:layout_width="match_parent"
android:layout_height="match_parent">

</ListView>

</RelativeLayout>


The ListView in this example displays contact names and phone numbers. So you need to define a layout file (contact_list_item.xml) that contains two TextViews. The layout file is used as a design template for each row of the ListView.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dip" >

<TextView
android:id="@+id/contact_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1sp"
android:textSize="20sp"
android:scrollHorizontally="true"
android:singleLine="true"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/phone_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1sp"
android:textSize="15sp"
android:scrollHorizontally="true"
android:singleLine="true"
android:textColor="#0000ff">
</TextView>

</LinearLayout>




You also have to create the ContactAdapter class that extends the ArrayAdapter class. The instance of the ContactAdapter class will be used to supply the contact information to the ListView. Here is the content of the ContactAdapter class.

  package com.example.andtip;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


public class Contactdapter extends ArrayAdapter<String>{
int groupid;
ArrayList<String> names;
ArrayList<String> numbers;
Context context;
String path;
public ContactAdapter(Context context, int vg, int id, ArrayList<String> names,ArrayList<String> numbers){
super(context,vg, id, names);
this.context=context;
groupid=vg;
this.names=names;
this.numbers=numbers;
}
public View getView(int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View itemView = inflater.inflate(groupid, parent, false);
TextView textName= (TextView) itemView.findViewById(R.id.contact_name);
String con_name=names.get(position);
textName.setText(con_name);
TextView textNumber = (TextView) itemView.findViewById(R.id.phone_number);
String number=numbers.get(position);
textNumber.setText(number);
return itemView;
}


}




In the MainActivty class, you need Java code to read contacts list from Android phones and display them in the ListView.

  package com.example.andtip;
import java.util.ArrayList;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.widget.ListView;
import android.widget.Toast;


public class MainActivity extends Activity {
Activity context;
private ArrayList<String> names;
private ArrayList<String> numbers;
private ContentResolver cr;
protected void onCreate(Bundle savedInstanceState) {
//TODO Auto-generated method stub
super.onCreate(savedInstanceState);
context=this;
setContentView(R.layout.activity_main);
//create ArrayList objects to store names and numbers
names=new ArrayList<String>();
numbers=new ArrayList<String>();

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public void onStart(){
super.onStart();
cr=getContentResolver();
String[] projection={ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,ContactsContract.Contacts.HAS_PHONE_NUMBER};
Cursor contacts=cr.query(ContactsContract.Contacts.CONTENT_URI, projection, null, null, null);
if(contacts.getCount()>0){
while(contacts.moveToNext()){
String contactId = contacts.getString(contacts.getColumnIndex(projection[0]));
String contactName=contacts.getString(contacts.getColumnIndex(projection[1]));
names.add(contactName);
String hasNumber=contacts.getString(contacts.getColumnIndex(projection[2]));
String nums="";
if(Integer.valueOf(hasNumber)==1){
Cursor numbers=cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while(numbers.moveToNext()){
if(nums=="")
nums=numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
else
nums+="/"+numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}

}

numbers.add(nums);

}
showContactList();
}
else{
Toast.makeText(context,"No contact", Toast.LENGTH_LONG).show();
}

contacts.close();


}

public void showContactList(){
ListView contact_list=(ListView) findViewById(R.id.contact_list);
if(contact_list!=null){
if(names.size()>0){
ContactAdapter contactAdapter=new ContactAdapter(this,R.layout.contact_list_item,R.id.contact_name,names,numbers);
contact_list.setAdapter(contactAdapter);
}
}
}


}



Before running the project, you need to add a permission to read the contact information in the AndroidManifest.xml file as below.

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

Android-showing-contacts-list

Posted by: Dara | post date: 06-09-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.