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

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