Android – Material Design Navigation Drawer

In this step by step tutorial, I will try to show how to implement Material Design Navigation Drawer. Okey, let’s start:

Step 1: add MaterialDrawer library to project
Open build.grade file and add this dependency.

    compile('com.mikepenz:materialdrawer:3.1.2@aar') {
        transitive = true
    }

And change minSdkVersion to 10. After that, your build.gradle file should something like this.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "net.joerichard.reader"
        minSdkVersion 10
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile('com.mikepenz:materialdrawer:3.1.2@aar') {
        transitive = true
    }
}

Step 2: take out ActionBar. Open res/values/styles.xml.
And replace this:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

To this:

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>

If you want to make Toolbar title white add following styles to styles.xml:

    <style name="ActionBarThemeOverlay" parent="">
        <item name="android:textColorPrimary">#fff</item>
        <item name="colorControlNormal">#fff</item>
        <item name="colorControlHighlight">#3fff</item>
    </style>

    <style name="HeaderBar">
        <item name="android:background">?colorPrimary</item>
    </style>

    <style name="ActionBarPopupThemeOverlay" parent="ThemeOverlay.AppCompat.Light" >
        <item name="android:background">@android:color/white</item>
        <item name="android:textColor">#000</item>
    </style>

Final version of styles.xml should look like this:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
    </style>

    <style name="ActionBarThemeOverlay" parent="">
        <item name="android:textColorPrimary">#fff</item>
        <item name="colorControlNormal">#fff</item>
        <item name="colorControlHighlight">#3fff</item>
    </style>

    <style name="HeaderBar">
        <item name="android:background">?colorPrimary</item>
    </style>

    <style name="ActionBarPopupThemeOverlay" parent="ThemeOverlay.AppCompat.Light" >
        <item name="android:background">@android:color/white</item>
        <item name="android:textColor">#000</item>
    </style>

</resources>

Step 3: add Toolbar. Open layout of your activity. In my case, it is res/layout/activity_main.xml. Add toolbar:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#BD601B"
        android:elevation="4dp"
        android:minHeight="?attr/actionBarSize"
        android:transitionName="actionBar"
        style="@style/HeaderBar"
        app:theme="@style/ActionBarThemeOverlay"
        app:popupTheme="@style/ActionBarPopupThemeOverlay"
        />

</RelativeLayout>

Result will be something like this:
Снимок экрана 2015-08-07 в 14.39.49

Step 4: add resources for Navigation Drawer(items’ titles and icons).
Open res/values/strings.xml and add following strings:

    <string name="drawer_title1">Моя библиотека</string>
    <string name="drawer_title2">Добавить книгу</string>
    <string name="drawer_title3">Настройки</string>
    <string name="drawer_title4">Оценить ридер</string>
    <string name="drawer_title5">Поделиться</string>

Then add following icons to res/drawable folder:
menu_icon1

menu_icon1

menu_icon2

menu_icon3

menu_icon5

Step 5: add toolbar to your activity. Open your activity and add reference your Toolbar onCreate method:

        final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Step 6: add navigation drawer onCreate method:

        DrawerBuilder builder = new DrawerBuilder()
                .withActivity(this)
                .withToolbar(toolbar)
                .addDrawerItems(
                        new SecondaryDrawerItem().withIcon(R.drawable.menu_icon1).withName(R.string.drawer_title1),
                        new SecondaryDrawerItem().withIcon(R.drawable.menu_icon2).withName(R.string.drawer_title2),
                        new SecondaryDrawerItem().withIcon(R.drawable.menu_icon3).withName(R.string.drawer_title3),
                        new SecondaryDrawerItem().withIcon(R.drawable.menu_icon4).withName(R.string.drawer_title4),
                        new SecondaryDrawerItem().withIcon(R.drawable.menu_icon5).withName(R.string.drawer_title5)
                )
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(AdapterView<?> parent, View view, int position, long id, IDrawerItem drawerItem) {

                        if (drawerItem != null) {
                            if (drawerItem instanceof Nameable) {
                                toolbar.setTitle(((Nameable)
                                        drawerItem).getNameRes());
                            }
                        }

                        return false;

                    }
                });

        Drawer drawer = builder.build();

That’s it! Let’s run application to see the result!
Снимок экрана 2015-08-07 в 16.27.50