This tutorial is about the navigation drawer in Android. The navigation drawer is a sliding panel that displays an app’s navigation options on the left side of the screen.  By default this is hidden, but it shows when the user swipes the screen from the left edge or touches the application on the action bar. Currently, lots of Android applications contain navigation drawers like the Google Play Store app, Gmail App, and etc.

Below you can find the screen shot of an Android sliding menu app using a navigation drawer.


The navigation drawer was launched in API level 11 (HONEYCOMB). But we can use it in older versions by using the support libraries.

On the left hand side of our Android navigation drawer app, we have listed Android versions in a sliding menu while on the right hand side, we will show the option selected by the user.

Listing 1: Create the file activity_main.xml.

  • We declared a navigation drawer widget named
  • We used a FrameLayout which will help to add fragments corresponding to the drawer items click.

Listing 2 : Create the new file drawer_list_item.xml.

Implementing a MainActivity for the Android navigation drawer

The MainActivity class is where we perform all of the navigation drawer setup. We create a new adapter in order to add items to the sliding menu. The complete code is after the explanation. Follow the steps given below to implement a navigation drawer:

  1. Set an adapter for the navigation drawer. We have created a string array and added the list of Android versions. This would be the content of our sliding menu.
    Snippet 1 :
  2. Now we want our application icon to respond to click events. It will toggle the navigation drawer.
    Snippet 2 :
  3. Now set up the navigation drawer listener. Mainly the toggle listener is used to handle events when the navigation drawer is open or closed.
    Snippet 3:
  4. Handle the click listener on the navigation drawer item
    We need to handle the click events. For that, we are implementing a “ListView.OnItemClickListener interface.
    Snippet 4 :

Let’s see the full code for the file.
Listing 3 :

If we want to show any layout or user interface, then for every item click listener, we need to open a fragment. Fragments are a portion of the user interface in an activity or we can say that fragments are a modular section of an activity. These all have different life cycles and we can add or remove fragments into the activity.
Snippet 5 :

Listing 4 : The layout file  fragment_version.xml


Conclusion :

This was all about implementing Android sliding menus using a navigation drawer. If you have any suggestions or received any errors while implementing this procedure, feel free to post in the comment section below.