Hello ! This is Bogdan with another awesome tutorial at www.androidgenuine.com.
Today I will show you how you can get contacts’ name and birthdays . Please notice that we’ll collect these information only from the contacts that are in phone memory, not in the SIM one.
Ok, let’s start. We’ll have only a class : ContactDetails.class and a XML file : mylayout.xml (I was not inspired here, sorry ).
Like always I will explain things using messages in the java code.
Here is the java class :
public class ContactDetails extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.mylayout); //Set the contentview layout TextView tvContactDetails = (TextView) findViewById(R.id.tvContactDetails); //Here is our textview Uri uri = ContactsContract.Data.CONTENT_URI; //We need this uri to get Contact Details String[] projection = new String[] { //Here we define the projection, a string where we write what we need to //take from the contacts ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Event.CONTACT_ID, ContactsContract.CommonDataKinds.Event.START_DATE }; String where = //here is another string which we will use . Here we put some conditions about the details //They can't be null and so on. It's a little bit harder this part ContactsContract.Data.MIMETYPE + "= ? AND " + ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY; String[] selectionArgs = new String[] {ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE}; String sortOrder = null; //sortOrder we want to be null. //To read all the contact details we need a cursor //Our cursor is a managedQuery with these properties. Cursor cursor = managedQuery(uri, projection, where, selectionArgs, sortOrder); //Here is the "while" where the interesting part will happen. //The clause is cursor.moveToNext, so what is in while will happen until there is no "next" element. while (cursor.moveToNext()) { //Here we get the two strings we are interested in String displayBirthday = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE)); String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); //We take only the contacts that have inserted a birthday . if(!displayBirthday.equals("")){ // We set the text to the textview. //We just add the new name and birthday. String was = tvContactDetails.getText().toString(); tvContactDetails.setText(was + name + " - " +displayBirthday+"\n"); } } }
Here is the xml file(we just have a textview) :
<?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/tvContactDetails" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </LinearLayout>
Here is a screen capture at this project :

Like always, I give the entire project . You can download it from here .
This is all for this tutorial. I hope you understood it and thank you for reading. See you !