Skip to content
This repository has been archived by the owner on Apr 30, 2020. It is now read-only.

Set Up a Navigation Drawer Activity

Fabio Biola edited this page Jan 25, 2015 · 7 revisions

Setting up an Activity is a three step process:

  1. Create the activity class
  2. Create the theme
  3. Connect all to the manifest

Step 1: Create the activity class

Create a java class with your IDE, then make it extends MaterialNavigationDrawer

public class MyNavigationDrawer extends MaterialNavigationDrawer {

    @Override
    public void init(Bundle savedInstanceState) {

    }
}

MaterialNavigationDrawer is an ActionBarActivity, but there are some important changes:

  1. you have an init method
  2. you must not override onCreate method
  3. you must not call setContentView method, because the library have it's own layout
  4. you must not override onBackPressed method, because the library implement it on its own

init method

This method is called into the ActionBarActivity onCreate method.
It is used for inserts the drawer list items (called Sections) and Accounts, if you are using the account drawer type.
If you are doing a migration from another navigation drawer, you should put all activity customization here.

N.B. You must have at least one section added to the drawer or it is useless.

Step 2: Create the theme

Now that your activity class is created, you should choose what type of drawer you want.
Open your styles.xml and add a new theme:

<style name="MyNavigationDrawerTheme" parent="MaterialNavigationDrawerTheme">
    <item name="colorPrimary">#8bc34a</item>
    <item name="colorPrimaryDark">#558b2f</item>
    <item name="colorAccent">#FFFFFF</item>
</style>

All themes should have colorPrimary,colorPrimaryDark and colorAccent item. They are added by AppCompat library for support all devices, from Froyo to Lollipop.

Like the official android themes, there are three types of the same theme:

  • MaterialNavigationDrawer (Dark Navigation Drawer)
  • MaterialNavigationDrawer.Light
  • MaterialNavigationDrawer.Light.DarkActionBar

For advanced theme customization, please go there: Advanced Theme Customization

Step 3: Connect all to the manifest

The final step, you have to declare into your manifest the activity:

<activity android:name="MyNavigationDrawer" android:theme="@style/MyNavigationDrawerTheme" />

Setup is complete!

Now you have a working navigation drawer.