Create View Delete List Contact – Android App

In my previous blog I explained how we can Transfer Data Between Activities Using Intent. I also explained the methods which are used for data transfer between activities in details.

You can find all my Android articles below.

In this blog we will create Simple Contact Android App. I will also explain how we can add a contact in ListView using Custom AdapterHow we can delete a contact from list view. In this project I used Context Menu. Using Context Menu we can Delete or View a particular contact.

While coding this project we will learn about some important concepts of Android like:

  • Intent.
  • Custom Adapter.
  • Context Menu and many more.

Before going forward it will be good to go through below blogs:

You can download the Contact Android App Project from below:

Before getting started, you need to know how we can create Menu when you Long Press on a particular contact.

  •   To create Menu we have to register for the View on which we have to make menu .

  • Then we override the method onCreateConetxtMenu() method.

  • We can say that this method create the menu, when you Long Press on View(in our case this is ListView).
  • menu.add(int groupId, int itemId, int order, charSequenceTitle) is used to add the menu.
  • groupId  allows you to categorize menu items so they share properties such as active state and visibility.
  • itemId Creates a Menu Item, which represents a single item in a menu. This element may contain a nested <Menu> element in order to create a sub menu.
  • order used to set the order of menu.
  • charSequenceTitleHere you can set Title of your menu.

To write the business logic on this menu we override onContextItemSelected().

  • We can say that this method is used to write the Business logic on Menu option.
  • Line 10 – 11: Here we get the position of Long Pressed contact in ListView.
  • Line 13: We sending the data when user select View option from menu into contactDetails page.
  • To create Custom Adapter we have to extends Base Adapter class. So i make an class named as ContactsAdapter which extend the base class Base Adapter.
  • I will explain about ContactsAdapter class in detail in down the line.

Create Contact Android App


So lets gets started with our Contact android App:

  • Open your Android Studio.
  • Click on Start a New Android Studio Project.
  • Write your Application Name “SimpleContactApp and leave other fields blank as it is, then click NEXT.
  • Select the Minimum SDK API 15: Android 4.0.3(IceCreamSandwich)”I selected API 15 because it covers 94% device and it has almost all the features. If you want to cover 100% device then you can select API 8: Android 2.2(Froyo). But it has limitation on features. After selecting your API click on NEXT.
  • Select the blank activity and click NEXT.
  • Leave the activity name “MainActivity” and leave everything as it is. Click Finish.
  • After clicking Finish, it takes around 5 minutes to build Activity.

Create Contact Class

  • Here I created a Contact class which implements Serializable interface.
  • This Contact class has the member function like name, email, number and image id.
  • We make this class so that we don’t have to work on individual elements.
  • We implemented serializable Interface so that we can send Object from one Activity to another Activity.
    • Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object’s data as well as information about the object’s type and the types of data stored in the object.
    • After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.

Create Data Class

  • After creating Contact class, I created Data Class which extends Activity.
  • In Data class, we take the inputs from user like Name, Number, Email and Profile Photo.


Below is the explanation of highlighted lines because that Java code  is behind working of Data class.

  • Line 34: Here we set the Click listener on Image view. So that it select the profile pictures.
  • Line 38: After clicking on image the User is to move ti Images Activity page where user select the profile picture of contact.
  • Here we used startActivityForResult() as we expecting some data back from Images Activity which is contact image selected by user.
  • The contact image we get from Images activity, we extract it using code Line 64-68.
  • Line 44: Here we set the click listener on SAVE button.
  • Line 48: Here we created the object of Contact class and called the Parameterised Constructor.
  • Line 52: Here we created the object of intent class.
  • We also set the source and destination of activity.
  • When user click the SAVE button, the activity goes from Data.Java to MainActivity.Java file.
  • Line 54: Here we send the Object of Contacts class.
  • We can’t send the Object of a class using Intent without implementing Serializable Interface.
  • Line 55: Here we set the result code as 2, and second argument as intent Object.
  • Line 64 – 68: Here we get the Image which is send from Images class.
  • We also set the contact image.

Below is the XML for Data class.

Contact Android App


Create Images class

  • Now we create Images class which extends Activity.
  • In this class we display profile pictures of user using Grid View.

Below is the explanation of highlighted lines

  • Line 17 – 21: Here we created the array of images.
  • Line 24: Here we created the object of AdapterImages class.
  • Line 33: Here we called the parameterized constructor of AdapterImages class.
  • Line 35: Here we set the Adapter.
  • Line 37 – 47: Here we set the click listener on GridView.
  • Line 43: Here we send the selected contact image by user.
  • Below i explained About AdapterImages class.

Create AdapterImages(Custom Adapter) Class

We also created the AdapterImages class which extends BaseAdapter , so that user can choose the profile picture from Grid View in a efficient manner.

Below is the explanation of highlighted lines:

  • To create an custom adapter class we must need to extends BaseAdapter class.
  • Here we created the AdapterImages to select the contact picture from GridView.
  • Here we implements four methods. getCount(), getItem(), getItemId() and getView().
  • When creating custom Adapter we must overrides this methods.
  • In this we only code in two methods, getCount() and getView().
  • Line 23 – 26: getCount() method returns the number of element a view have. Here it will return the number of images GridView have.
  • For CustomAdapters getView methods is most important. Here we code the main logic.
  • Line 38 – 53: getView method have return type View, which is GridView here.
  • Here we want to show images in GridView.
  • So here we created the object of ImageView class if ConvertView is Null.
  • Here what happens, gridView request image for the 0th Position, then AdapterImages return the image for the 0th Position.
  • The number of images fit into the screen, until then Convert View is NULL. After that convert view just used the same id to send the images.
  • Now you understand the logic between Line 42 -50.

Below is the XML for Images class.

Contact Android App


  • After that we created ContactDetails class files which is used to display the selected contact.
  • In ContactDetails Class we used TextView to display name, number and Email.
  • ImageView is used to show the profile picture.


  • Now we code MainActivity.Java file, which is our first screen when user open Contact Android App.
  • In MainActivity file we implemented, what happened when user come first time.
  • We also implemented what happened when user do Long press on contacts.

Below I explained about the code MainActivity.Java file

  • Line 25: Here we used ArrayList generic class.
  • We used ArrayList because its size can be increased or reduced dynamically.
  • Line 43 – 52: Here we set the Click Listener on Contact Button.
  • When user click on Contact Button, it takes user to the Data Activity, where we take input from the user.
  • Line 54: Here we called the constructor of ContactsAdpater class, in  which we passed the context and ArrayList.
  • Here Context  can be defined as Current View which provide all the basic things to create an activity.
  • Line 81 – 115: Here i implemented the Menu option, when user do a long Press on contacts.
  • Line 70 – 77: In onCreateContextMenu() we write code for , what Menu we need when user render the long press.
  • Line 81 – 115: In onContextItemSelected(), we write the code for each Menu.
  • So here we use Switch case, for each element.
Contact Android App


  • Line 118 -128: In onActivityResult(), we receive the data coming from Data Activity.

Create ContactsAdapter(Custom Adapter) Class

  • We need to Create ContactsAdapter class which take the contacts as Object and return to the MainActivity file with all the details.
  • This class is almost similar to ImagesAdapter class which i explained above.
  • Here just we have taken ArrayList object with return type Contact as member of class.

  • Line 49: Here we used LayoutInflater class, which is used to wrap up some view into one.
  • As we know, here we need TextView for name ,TextView for number, TextView for email and ImageView for profile picture.
  • So instead of creating object of each and setting attributes(By creating object of each and setting attributes for each View, the code become lengthy) of each we make an XML file lay_contact.XML, which have all the views.
  • We just have to Inflate using LayoutInflater class. Because of this, the code is more readable.
  • Line 50: Here in the arguments, we have given the XML file which has to be Inflate.

Below is the lay_contact.xml, which represents how a particular ListView look like.


Below is the Activity_Main.XML



We are ready to run our Contact Android app. To run our app I used Genymotion Emulator as it’s fast. Also its performance is better than default Android emulator.

If you have not already installed, I highly recommend Genymotion emulator. You can install from here: Android Emulator Genymotion Step by Step Installation. If you face any errors while installing android studio this is a good reference point.

Running our Contact Android App

  • Click on Genymotion device manager. After selecting your custom device in Genymotion device manager window, click START.
  • Click on Run.
  • Choose Genymotion device and click OK.
  • Now you can see Contact android app running.
Contact Android App

Output 1



Contact Android App

Output 2


Contact Android App

Output 3


Contact Android App

Output 4


Contact Android App

Output 5

Congratulations! If you reach this point means you have followed all the steps correctly and your contact android app is running fine. If you are a beginner and learning android, I recommend go through below tutorial to get you speed up.

If I missed any point or you have any doubt regarding this blog just let me know in the comment section. If you like my article please like our Facebook page and also follow us on Twitter so that you get regular updates. For regular updates you can also subscribe to with your email.

Please also share on Facebook and Twitter to help other android developers. Thank You..!