Android How to - Read contact list








The following code shows how to read contact list.

Example

manifest xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.contactlist"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ContactList"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".ContactDetails" android:label="@string/app_name"></activity>
    </application>
    <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
</manifest> 

layout xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="vertical">

  <TextView android:id="@+id/contact_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

  <TextView android:id="@+id/contact_phone"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

</LinearLayout>

Java code

import android.app.ListActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;
//w  w w  . jav a2  s.  c  o m
public class ContactList extends ListActivity implements OnItemClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
      
        super.onCreate(savedInstanceState);
        
        String[] projection = new String[] { People._ID, People.NAME };
        Cursor cursor = managedQuery(People.CONTENT_URI, projection, null, null, People.NAME + " ASC");
        
        ListAdapter adapter = new SimpleCursorAdapter(
          this,
          android.R.layout.two_line_list_item,
          cursor,
          new String[] { People._ID, People.NAME },
          new int[] { android.R.id.text1, android.R.id.text2 }
        );
        setListAdapter(adapter);
        
        getListView().setOnItemClickListener(this);
        
        
    }

  public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
    
    Uri contactUri = ContentUris.withAppendedId(People.CONTENT_URI, id);
    Intent intent = new Intent(this, ContactDetails.class);
    intent.setData(contactUri);
    startActivity(intent);
    
  }
    
}
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.TextView;
/*from  w  w  w . j a  v a 2  s .  c o  m*/
public class ContactDetails extends Activity {
  
  TextView nameField = null;
  TextView phoneField = null;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
      
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details);
        
        nameField = (TextView) findViewById(R.id.contact_name);
        phoneField = (TextView) findViewById(R.id.contact_phone);
        
    }

  @Override
  protected void onStart() {
    
    super.onStart();
    
    Cursor cursor = managedQuery(getIntent().getData(), null, null, null, null);
    cursor.moveToFirst();
    
    nameField.setText(cursor.getString(cursor.getColumnIndex(People.NAME)));
    phoneField.setText(cursor.getString(cursor.getColumnIndex(People.NUMBER)));
    
  }

}