Home > Tutorials > Create a simple phone book

Create a simple phone book

How about to create your own custom phone book displaying like this:

Phone Book

Phone Book

It isn’t complicated, is it? As you see, every item in the list view contains an image and 3 lines of text indicating Name, Phone Number and Email Address.

Ok, so let’s start to create!

A – Create The Project

Project Name: Phone Book

Application Name: Phone Book

Package Name: pete.android.study

Create Activity: MainActivity

Min SDK: 10

Click OK -> Done with creating project.

B – Sketching The Layout

As you see in the image above, apparently, we need two layouts.

+ One layout for main screen display, which is the list view

+ One layout for each item in the list view, which will be set to list view

First, we start with layout for each item in list view.

1. List Item Layout

What we have in here? The profile avatar on the left of the list item, we will use a ImageView control to display.

On the right, there are three lines of text, one is for Name, one for Mobile Number and the other for Email Address.

Of course, we can use just one single TextView to display all three lines but in my opinion, it would be very inconvenient when we need to reset data text to display on one of these three lines. So, it’s better to use 3 TextView controls, one for each.

For layout display, we need one layout container for 3 lines of text and one layout container for the ImageView and the other layout container (containing 3 TextView).

Hence, the final layout will be like this:

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








2. Main Layout

Simply, just one ListView to display all items.

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



C – Class Design: On the Idea

Class Diagram

Class Diagram

D – From Design to Coding

1. PhoneBook: the entity class which holds the information: avatar bitmap, name, phone, email.

package pete.android.study;

import android.graphics.Bitmap;

public class PhoneBook {
	private Bitmap mAvatar;
	private String mName;
	private String mPhone;
	private String mEmail;

	public PhoneBook(Bitmap avatar, String name, String phone, String email) {
		mAvatar = avatar;
		mName = name;
		mPhone = phone;
		mEmail = email;

	public void setAvatar(Bitmap avatar) {
		mAvatar = avatar;
	public Bitmap getAvatar() {
		return mAvatar;

	public void setName(String name) {
		mName = name;
	public String getName() {
		return mName;

	public void setPhone(String phone) {
		mPhone = phone;
	public String getPhone() {
		return mPhone;

	public void setEmail(String email) {
		mEmail = email;
	public String getEmail() {
		return mEmail;

2. PhoneBookAdapter: the adapter used to handle data when it is changed. However, I’ve just wanted to show how to create the custom list view, so I won’t mention about handling list item data.

package pete.android.study;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PhoneBookAdapter extends BaseAdapter {
	private Context mContext;
	private List<PhoneBook> mListPhoneBook;

	public PhoneBookAdapter(Context context, List<PhoneBook> list) {
		mContext = context;
		mListPhoneBook = list;


	public int getCount() {
		return mListPhoneBook.size();

	public Object getItem(int pos) {
		return mListPhoneBook.get(pos);

	public long getItemId(int pos) {
		return pos;

	public View getView(int pos, View convertView, ViewGroup parent) {
		// get selected entry
		PhoneBook entry = mListPhoneBook.get(pos);

		// inflating list view layout if null
		if(convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			convertView = inflater.inflate(R.layout.phonebook_row, null);

		// set avatar
		ImageView ivAvatar = (ImageView)convertView.findViewById(R.id.imgAvatar);

		// set name
		TextView tvName = (TextView)convertView.findViewById(R.id.tvName);

		// set phone
		TextView tvPhone = (TextView)convertView.findViewById(R.id.tvPhone);

		// set email
		TextView tvEmail = (TextView)convertView.findViewById(R.id.tvEmail);

		return convertView;


3. MainActivity: this is the entry point of application to create and display the list, which will use “<<override>>onCreate()” method.

package pete.android.study;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {

	private ListView lvPhone;

    public void onCreate(Bundle savedInstanceState) {

        lvPhone = (ListView)findViewById(R.id.listPhone);

        List<PhoneBook> listPhoneBook = new ArrayList<PhoneBook>();
        listPhoneBook.add(new PhoneBook(
        		BitmapFactory.decodeResource(getResources(), R.drawable.avatar_pete),
        		"Pete Houston", "010-9817-6331", "pete.houston.17187@gmail.com"));
        listPhoneBook.add(new PhoneBook(
        		BitmapFactory.decodeResource(getResources(), R.drawable.avatar_lina),
        		"Lina Cheng", "046-7764-1142", "lina.cheng011@sunny.com"));
        listPhoneBook.add(new PhoneBook(
        		BitmapFactory.decodeResource(getResources(), R.drawable.avatar_jenny),
        		"Jenny Nguyen", "0913-223-498", "jenny_in_love98@yahoo.com"));
        PhoneBookAdapter adapter = new PhoneBookAdapter(this, listPhoneBook);

E – Note

– The image display in the ImageView is scaled by setting in the layout, which uses “dip” unit.

– Use BitmapFactory.decodeFromResource() to create a bitmap from resource.

– Always use an adapter to use in the ListView to handle data change events (display, add, remove…). However, I don’t mention about it here. Just remember it!


F – Get The Project Created by Pete

Pick your favorite hosting server: Mediafire | Rapidshare | FreeFileHosting


G – Final Words

– Did you learn something from this?

– Hope you enjoy it 🙂



Pete Houston

Categories: Tutorials Tags: , ,
  1. January 31, 2017 at 8:43 pm

    Great tutorial! I want to make one for my large extended family. So how can I add a phone call button and an email button?

  2. December 20, 2016 at 8:48 pm

    phonebook_row is an error i think so…. can you respond to this

  3. sex
    July 16, 2016 at 4:25 pm

    Very nice article. I definitely love this site. Continue
    the good work!

  4. Shubham H
    May 23, 2015 at 1:56 am

    Hello Sir,
    Can you help me in updating the code?? how could I implement ‘Add Contact’ feature???

  5. December 27, 2014 at 11:52 am

    balveer like it

  6. September 18, 2014 at 1:58 pm

    you are truly a good webmaster. The web site loading velocity is incredible.
    It sort of feels that you’re doing any unique trick.
    Furthermore, The contents are masterpiece. you have done a excellent job on this topic!

  7. September 18, 2014 at 1:33 pm

    Great info. Lucky me I recently found your website by chance (stumbleupon).
    I’ve book marked it for later!

  8. September 4, 2014 at 2:20 pm

    Hi my family member! I want to say that this post is awesome, nice written and come with almost all vital infos.
    I’d like to see extra posts like this .

  9. August 20, 2014 at 4:37 am

    This is the right site for anybody who would like to find out about this
    topic. You know a whole lot its almost tough to argue with you (not
    that I personally would want to…HaHa). You certainly put a new spin on a topic that’s been written about for ages.
    Excellent stuff, just great!

  10. Phil
    July 25, 2014 at 6:57 am

    Very useful info! I’m just learning Android, and this shows a lot of what I need to learn about structures of the language!
    Thanks for this!

  11. March 3, 2014 at 10:09 am

    Hi there Dear, are you really visiting this website on a regular basis, if so
    afterward you will without doubt obtain pleasant know-how.

  12. Kasun Lakamal
    February 8, 2014 at 10:47 pm

    thank lot

  13. jeetu
    June 14, 2013 at 5:55 pm

    hey who ever written this code i like a scrap u use 1 name in in adapter file and for same method you used some other name in phoneBook and in main activity file u find methos to some other name what all these yaaaaaaaaaaaaar

    i only say scrap

  14. irfan
    August 1, 2012 at 2:27 pm

    gu sir it is a very easy code

  15. ibrahim
    June 11, 2012 at 10:22 pm

    great work but i just want to ask u one thing please can u creat phone book with database that i would put it in external sd card if u can blease just tell me what type of database should be
    becausec i already have database and it so large 1giga bytes its sql blease help me

  16. January 17, 2012 at 8:24 pm

    I want to make a website with wordpress, where my user can save their phonebook in my database and able to collect these phonebook when need. They can also able to add multiple or single number of phone number at any time.
    Please suggest me if you can.
    Thanks a lot.

  1. February 12, 2015 at 12:54 am
  2. August 25, 2014 at 1:55 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: