How to create Android Contact Book App : Part 3 : Adding Contact Data to SQLite Database

In the previous parts, we have already created Layouts and Contact class and SQLite Database handler class to Store contacts into database.

HOW TO CREATE ANDROID CONTACT BOOK APP : PART 1 : SETTING UP INTERFACE
HOW TO CREATE ANDROID CONTACT BOOK APP : PART 2 : CREATING SQLITE DATABASE

In this part we will see how we can get input data from user via Add Contact form and store the input contact information to SQLite database.

Table of content


  1. Creating Activity to Add New Contact
  2. Setting Layout for Activity
  3. How to Add Activity into AndroidManifest.xml
  4. How to Navigate Main Activity to Add New Contact Activity
  5. Defining SQLite Database Contact Handler
  6. How to Pickup Contact Person Image from Gallery
  7. Getting User Input Data
  8. Pushing Contact Data to Database
  9. Reading Contacts from Main Activity
  10. Download Start Source
  11. Download Final Source

1. Creating Activity to Add New Contact

Download and import Contact Book start project.
Right click on src>New>Class, name it “NewContact.java” and set Superclass as “android.app.Activity”.
NewContact.java

2. Setting Layout for Activity

Now Set up new contact layout (res>layout>new_contact.xml) which we have created already. see Part 1 : Setting up Interface.

3. How to Add Activity into AndroidManifest.xml

Open AndroidManifest.xml and New Contact Activity under application node.

4. How to Navigate Main Activity to New Contact Activity

To load NewContact Activity, we have to implement logic into Main.java. See below

How to Navigate Main Activity to New Contact Activity

How to Navigate Main Activity to New Contact Activity

5. Define SQLite Database Contact Handler

Now define the database handler which we have created in Previous part. i.e. ContactHandler Class

Then initiat it into onCreate() method

6. How to Pickup Contact Person Image from Gallery

If you notice, In Add new contact form there is ImageView to set contact image too. But how we can set that image. Let’s see how.
Add on click listener to ImageView widget into onCreate() method.

How to Pickup Contact Person Image from Gallery

How to Pickup Contact Person Image from Gallery

Then override onActivityResult() function to get the result and set the Picked image to ImageView.

How to Pickup Contact Person Image from Gallery

How to Pickup Contact Person Image from Gallery

7. Get User Input Data

Now get the user input data. But before that we will define variables for each individual contact information.

Now set click listener to “Add Contact” button

Now get all the input data on Click.

8. Pushing Contact Data to Database

Here is the code for pushing contact data into database and jump to Main Activity, if contact information is inserted successfully into database.

9. Reading Contacts from Main Activity

Here after adding contact we are loading Main activity. But how we can confirm the contact is added. Let’s check with LogCat.
Define the new function into Main.Java

And call this function into onCreate() method of the Main.java.
Run the application on emulator, add contact data and see the result into LogCat.

Reading Contacts from Main Activity

Reading Contacts from Main Activity

Complete Main.java

Complete NewContact.java

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *