Android Basics : New App to demonstrate SQLite Database Concepts

To understand how to use SQLite with your App, you will be creating a new App.

The App will be called SurveyApp. And it will let users fill in few details and store it in the database.

Follow the video to get started. The new App source code is available here.

Android Basics – Introduction to SQLite Database

Why you need a Database?

A scenario can be if your app needs to store data. Like when a user registers then you can save the information for later use.

There can be other reasons also. For example if a user has weak or no network then your app can display the last saved data to the user.
What is SQLite Database?

Android comes with a lighweight database which can be used with your apps. SQLite is an open source lighweight database.

SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.

Why use SQLite?

1. It’s lightweight – It uses a file to store data. So no processing time is needed and hence you conserve battery.

2. It’s fast and stable and can handle transactions.

3. It’s optimized for a single user.

How does SQLite store data?

Android creates a data folder for each app where the files reside. Two files are created to store the data.


This folder can only be accessed by the App.

Reference –

Learn how to use the OnBackStackChangedListener to get the current Fragment

Suppose your App Title changes based on the Fragment name. So if you have three fragments FragmentA, FragmentB and FragmentC then the title will be based on the Fragment being displayed.

Like if you are at FragmentA then the App title will become FragmentA and so on.

Now if you navigate from FragmentA to FragmentB and then click on back button how will you know which Fragment you are at.

To solve this problem you can add a OnBackStackChangedListener.

Let’s see the steps to do this. You can refer to the source code for this illustration here.

1. When you replace a fragment in the code then attach a name to it.

ft.replace(, tf, “visible_fragment”);

2. Add a Backstack Listener

getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
public void onBackStackChanged() {
Fragment currentBackStackFragment = getFragmentManager().findFragmentByTag(“visible_fragment”);
if(currentBackStackFragment instanceof TopFragment){
//Add Code

This listener will be called when you click on the back button. The logic will help you to identify the Fragment.

So go ahead and try this out.

Learn how to add a Toggle Button to the Navigation Drawer

In order to add the Toggle button to your App you need to follow these steps

1. Add the following code to the Activity class


2. Override the onPostCreate method

protected void onPostCreate(Bundle savedInstanceState) {

3. Override the onConfigurationChanged method

public void onConfigurationChanged(Configuration newConfig){

4. Add following snippet to onOptionsItemSelected

return true;

Follow the Video to gain better understanding. You can refer to the source code here

Learn how to use ActionBarDrawerToggle to handle DrawerLayout events

In the last article you saw how to use DrawerLayout to create NavigationDrawer. Once you have a NavigationDrawer you may want to handle the drawer open and close events.

Like if the DrawerLayout is open you may like to display a different title and also hide some menu items.

In order to handle such events you need to create a ActionBarDrawerToggle object and assign it as a Drawer Listener.

Follow the video and refer to the Source code here

You don’t follow your Passion, it follows You

These are the lines spoken by Terri Trespicio at a TED talk where she recommends to stop searching for passion and start doing stuff. Can’t agree with her more.

Our society is plagued with this philosophy and this idea sells quite well. Not to say that its wrong but you loose out opportunities in this process.

Instead its way better to focus your energy on the job at hand.

It’s a very thoughtful and inspiring talk and I will highly recommend it.

Image Courtesy – TED Talk

Learn how to add Navigation Drawer to your Android App

What is a Navigation Drawer?

The navigation drawer is a panel that displays the app’s main navigation options on the left edge of the screen. It is hidden most of the time, but is revealed when the user swipes a finger from the left edge of the screen or, while at the top level of the app, the user touches the app icon in the action bar.

You can learn more at Google developers guide.

Why use a Navigation Drawer?

Say you have an e-commerce app. You can use the Navigation Drawer to display Categories and Sub Categories. Similarly for any app you can provide a set of shortcuts to the user.

How to add Navigation Drawer?

In order to add this you need to modify the layout xml file. To the layout xml file you need to add a Drawer Layout.  You will need to add v4 support library, if you don’t have it.





Follow the video in which I will guide you step by step to add this feature.

Source Code is available here

Learn How to Enable Up Navigation Button in Your App

In one of the earlier article you have seen how the back button works. The user can navigate through activity history by the back button click.

The up button is different. It allows you to traverse through the Activity hierarchy.

Let’s say you have MainActivity. From MainActivity you navigate to DetailActivity. Now if you have declared MainActivity to be a parent of DetailActivity then you can navigate back to it using an up button.

To illustrate this I have added a DetailActivity to the ThemeDemoApp, as shown in the video.

Please follow the video to understand this better and you can refer to the source code for the example App here.

Why do you want to Learn something ?

“If you can explain the WHY of things then that makes a huge difference to People’s Motivation. They understand Purpose. Process of Learning and how it should be. Make it interesting.” – Elon Musk

As Elon Musk has rightly said answer to the above question can be very useful to build and keep your interest to Learn something.

Elon also mentions the use of Gamification to make learning fun.

Here is a video in which Elon Musk talks about this and more at the Khan Academy.

Image and Video Source – Youtube – Khan Academy

I wrote a whole Post on this topic sometime back. You might benefit from reading that.