62

I see that Android introduced new navigation drawer icons, drawer icon and back arrow icon. How can we use that in Kitkat supported apps. See Google's latest version of Newsstand app, which has the latest navigation drawer icons and animations. How can we implement that?

I have tried setting the minSDK to 19 and complileSDK to 21 but it's using the old style icons. Is that self implemented?

tomrozb
  • 25,773
  • 31
  • 101
  • 122
nomongo
  • 3,435
  • 7
  • 30
  • 33

4 Answers4

130

You need to use the new Toolbar in the appcompat v21 and the new ActionBarDrawerToggle that is in this library as well.

Add the gradle dependency to your gradle file:

compile 'com.android.support:appcompat-v7:21.0.0'

Your activity_main.xml layout would look something like that:

<!--I use android:fitsSystemWindows because I am changing the color of the statusbar as well-->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_parent_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true">

    <include layout="@layout/toolbar"/>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Main layout -->
        <FrameLayout
            android:id="@+id/main_fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <!-- Nav drawer -->
        <fragment
            android:id="@+id/fragment_drawer"
            android:name="com.example.packagename.DrawerFragment"
            android:layout_width="@dimen/drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="left|start" />
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

Your Toolbar layout would look something like that:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"/>

Your activity must extend from:

ActionBarActivity

When you find your views (drawer and toolbar) in the activity the set the toolbar as the support action bar and set the setDrawerListener:

setSupportActionBar(mToolbar);
mDrawerToggle= new ActionBarDrawerToggle(this, mDrawerLayout,mToolbar, R.string.app_name, R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);

After that you just need to take care of the menu items and drawerToogle state:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = new MenuInflater(this);
    inflater.inflate(R.menu.menu_main,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

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

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

@Override
public void onBackPressed() {
    if(mDrawerLayout.isDrawerOpen(Gravity.START|Gravity.LEFT)){
        mDrawerLayout.closeDrawers();
        return;
    }
    super.onBackPressed();
}

The implementation is the same as It was before the Toolbar and you receive the arrow animation for free. No headaches. For more information follow:

If you want to display the drawer over the Toolbar and under the status bar, please refer to this question.

EDIT: Use NavigationView from the support design library. Tutorial to learn how to use in here: http://antonioleiva.com/navigation-view/

Community
  • 1
  • 1
jpardogo
  • 5,636
  • 2
  • 23
  • 27
  • 39
    I have a question... what is the purpose of this awesome ArrowDrawable when Google recommends in Material Spec that Drawer should be above Actionbar/Toolbar? – Michał Z. Oct 24 '14 at 15:46
  • To be honest, I don't know. But you can find a good discussion about it in this google+ post: https://plus.google.com/102272971619910906878/posts/CQW5Wejx44g – jpardogo Oct 26 '14 at 15:17
  • 1
    @MichałZ. Actually there's a pretty easy explanation for this: "Ink on paper doesn't transfer itself between pieces of paper." [quote from Adam Powell (click)](https://plus.google.com/117029492535934498672/posts/9PSi9FGdKeW). And as you may know: Pieces of paper that are moving over each other is one of the core-concepts of Material Design. – reVerse Oct 31 '14 at 12:24
  • 2
    Google's Apps has different Navigation Drawers: Play store has this fancy arrowdrawable,newsstand has the material design which moves actionbar. http://www.androidpolice.com/2014/10/30/google-turns-design-inconsistency-ten-latest-round-navigation-drawers/ – Dory Nov 03 '14 at 05:16
  • I followed your answer but it does not show any icon with or without animation – Ravi Nov 05 '14 at 09:29
  • Thanks - that works perfectly! But is there a way to get rid of the arrow animation? I am following the newest guidelines and now I see the hamburger menu transforming to an arrow below my navigation drawer... :( – Zordid Nov 10 '14 at 17:30
  • Check this answer: http://stackoverflow.com/questions/26439572/how-to-style-the-drawerarrowtoggle-from-android-appcompat-v7-21-library . With false you could at least deactivate the rotation. – jpardogo Nov 18 '14 at 16:58
  • Can we have a custom view for toolbar,say I want the drawer icon to align to right. Using action bar we can set any custom view we want . How can this be done in toolbar, as there is no `setCustomView()` in toolbar ? – Dory Dec 10 '14 at 06:07
  • This answer worked for the original poster, but not for me. Can someone help? http://stackoverflow.com/q/27761660/3038563 – wasimsandhu Jan 04 '15 at 04:44
  • @jpardogo works like charm, but isn't the nav drawer suppose to overlay the actionbar/toolbar. something like this http://www.google.com/design/spec/patterns/navigation-drawer.html – T_C Feb 15 '15 at 03:08
  • can i do this with eclipse ? – Lahiru Prasanna Apr 10 '15 at 03:13
  • 3
    Really good answer! But ActionBarActivity should be edited to AppCompatActivity. – Jesson Atherton Jun 19 '15 at 23:15
  • I edited the answer time ago, please read the answer until the end. NavigationView – jpardogo Aug 06 '15 at 14:57
18

The answer is no longer useful. Leaving it here for only historic purpose as the time of posting Android did not have the implementation :)


There are plenty of libraries now that can achieve this.

Choice 1 - https://github.com/neokree/MaterialNavigationDrawer

Others

appbootup
  • 9,537
  • 3
  • 33
  • 65
  • I would strongly advise Reimer's project first, and not neokree's. The former correctly puts the navigation drawer behind the task bar, includes all the necessary source and is able to build the project, which the latter fails on all accounts. Though Reimer's is currently not working on 5.1 and still contains bugs. – RedGlyph Apr 24 '15 at 11:59
  • 1
    Scratch that, Reimer's code isn't mature enough and probably wants to tackle too many things that still need debugging, which doesn't make it a didactic example. https://github.com/kanytu/android-material-drawer-template is a better example but it's not maintained anymore, in favour of his latest version (https://github.com/kanytu/Android-studio-material-template), which is actually a template for Android Studio! Much better to start from there IMHO. – RedGlyph Apr 24 '15 at 13:42
  • first choice is questionable — having to extend a class other than ActionBarActivity is PITA, because then you can't have a common base class for activities – Display Name May 16 '15 at 20:01
2

If you want the real navigation drawer with material design style (defined here)
I have implemented a custom library that do exactly that.
You can find it here

NeoKree
  • 411
  • 4
  • 13
  • can i do it eclipse rather than android studio..? because i need to this navigation drawer – GvSharma Jan 21 '15 at 14:14
  • Sure, but you have to import manually all dependencies and codes. – NeoKree Jan 22 '15 at 19:53
  • It seems to have quite a few issues, have you tested the build on a fresh clone? From where I stand, the project directories being on several levels, you need at least to add this to your example settings.gradle: `project(':MaterialNavigationDrawerModule').projectDir = new File('../MaterialNavigationDrawerModule')`. Then it tries to import a `import it.neokree.example.backpattern.BackPatternCustom;` that doesn't exist. Still 93 open issues. It's nice to provide a library ... if it's solid. – RedGlyph Apr 24 '15 at 09:18
0

Supporting top comment along with the new generated main_content's layout. I simply override the included content layout with DrawerLayout. Keep in mind that your drawerlayout must have this layout_behavior: appbar_scrolling_view_behavior

top container's layout https://github.com/juanmendez/jm_android_dev/blob/master/01.fragments/06.fragments_with_rx/app/src/main/res/layout/activity_recycler.xml#L17

included content layout https://github.com/juanmendez/jm_android_dev/blob/master/01.fragments/06.fragments_with_rx/app/src/main/res/layout/content_recycler.xml#L9

see the navigation drawer!!

Juan Mendez
  • 2,658
  • 1
  • 27
  • 23