Course Content

Course Content


How To Set Up Address Book On Android Phone

Till now we have seen many concept like in Database we can write data using Internal storage, Shared preferences etc... Now we will see that how we can apply SQLite practically.

First of all we will create a project named 'AddressBook' in our Android studio. And after that we will open its 'AndroidManifest.xml' file which is default generated, we have just added an icon, previously we used to have default android icon but now we have stored an icon in drawable folder so that is why we are going to display our own application icon in this case.

Now we will open our 'main.xml' file which is inside our layout folder to decide the designing of our application. Designing for this application is quite lengthy, but easy to understand.

So first of all we have taken an absolute layout here, and created an 'Exit' button with its usual properties. Now we will use a new concept 'ViewFlipper' here, in which we will use four different TableLayout. To change to different view from one to another we will use the concept of viewflipper for our application.

Now we will design our tag for viewflipper in our 'main.xml' file,






Since we have designed ViewFlipper's basic properties, we will create TableLayout, in which we will use one 'ListView' and two 'Buttons' to add or delete any content.

Now in our second TableLayout, we have used 'TextView', to display the details of what user selected in the first activity.

After that in our third TableLayout, we need to designe a 'delete' button, sofa that we will add content here. And in this layout we will also have back button.

Then in next TableLayout we have simply used buttons, Button 'back' and Button 'save'.

Now we will design it's code which is inside the 'java' folder, here we have two classes, one main activitiy in which are main code will be stored, and other one is 'DataHelper' in which we will generate code to create database.

So first we will open our DataHelper class in which we will see that we have declared some kind of strings,

private static final String DATABASE_NAME=""AddressBook.db"";

private static final int DATABASE_VERSION=1;

private static final String TABLE_NAME=""AddressContact"";

private Context context;

private SQLiteDatabase db;

private SQLiteStatement insertStat;

private static final String INSERT=""insert into""+TABLE_NAME+""(name.address)value (?,?)"";


Now we will see that how we can create database using DataHelper,


private static class OpenHelper extends SQLiteOpenHelper


OpenHelper (Context context)


super(context, DATABASE_NAME, null, DATABASE_VERSION);



public void onCreate (SQLiteDatabase db)

{ db.execSQL(""CREATE TABLE"",TABLE_NAME,""(id INTEGER PRIMARY KEY, name TEXT, address TEXT)"");



public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)


Log.v(""Address"",""Upgrading database, this will drop tables and recreate."");





Now we will design the constructor of class DataHelper,


public DataHelper(Context context)



OpenHelper.openHelper=new OpenHelper(this.context);





To insert the content using a method,


public long insert (String name, String address)




this.insertStat.bindString(1, name);

this.insertStat.bindString(2, address);

return this.InsertStat.executeInsert();


catch(SQLException ex)


return 0;




To delete data from database name and address,


public void delete (String id) {



public void deleteAll()


this.db.delete(TABLE_NAME,null, null);



Any content which is getting added or deleted should have a connection with the list which should be in database, so to establish that connection,


public List<String> selectAll()


List<String> list=new ArrayList<>();

Cursor cursor=this.db.query(TABLE_NAME, new String[] {""name""}, null, null, null, null, ""name desc"");

if {cursor.saveToFirst()}






While (cursor.moveToNext());


if {cursor != null && !cursor.isClosed()




return list;



To select a specific content we can generate code with help of 'cursor',


Cursor cursor=this.db.query{TABLE_NAME, new String[]




""name='""+name+"",null, null, null, null



Now we will look in the main activity file, the first thing which we we will do is to declare all the other contents which we used, so,


private dh;

private ViewFlipper vf;

ListView listview=null;


Now in in 'onCreate' we have done basic things like view identification for edittext, viewflipper, list view, button OnClickListener.


Now to flip between different views we have used viewflipper in following way,

public void onClick (View V)








After that we have done identification of our exit button, which will simply finish the activity.

Now we will see that how 'Save' button works, so for that, first we will look that is both of our text boxes filled with content, if not then we will use alert box to display 'All fields are mandatory'. When text is passed, it will show notification that 'New contact saved successfully'.

Now in last we have used fillListView(), in which with the help of Adapter we have fill the content.

Now when we execute our code, it runs successfully as we needed.


Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
297K+ Downloads
App Download Section Circle 1
4.5 Rating
App Download Section Circle 2
10K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere