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.

/data/data/{unique-domain-name}/databases/example-db

This folder can only be accessed by the App.

Reference – https://www.sqlite.org/

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(R.id.frameLayoutId, tf, “visible_fragment”);

2. Add a Backstack Listener

getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
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

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

2. Override the onPostCreate method

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mActionBarDrawerToggle.syncState();
}

3. Override the onConfigurationChanged method

@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
mActionBarDrawerToggle.onConfigurationChanged(newConfig);
}

4. Add following snippet to onOptionsItemSelected

if(mActionBarDrawerToggle.onOptionsItemSelected(item)){
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.

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawerLayoutId"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/frameLayoutId"/>

    <ListView
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:id="@+id/drawerLayoutListId"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#ffffff"
        android:layout_gravity="start"
        />

</android.support.v4.widget.DrawerLayout>

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

Source Code is available here