Currently at the moment there isn’t really a guide on how to get a fully material design compliant drawer on android 5.0. So this is going to be a walkthrough on how to do exactly that.
Setup
Your targeting api should probably be v21+ (or android 5.0+) and you can either do no activities or a blank activity. You will want to immediately check out your build.gradle and make sure you have the appcompat dependency is in there. (Note: this is usually automatically included for you).
Along with making sure your main theme is an AppCompat theme, not the device default theme.Inside of that theme make sure you have windowActionBar set to false.
Replacing the ActionBar
Lets start with how to get the drawer out from under the action bar. By default when you add a drawer to your application it will appear under your action bar. To rectify this, you need to use a toolbar instead of the default ActionBar. When you added windowActionBar to your theme, you where actually turning that actionbar off. To get back that actionBar we need to change the activity to AppCompatActivity.
Now that our activity extends AppCompatActivity we need to go and add a Toolbar to this activity’s layout. It should look something like this:
We have the Toolbar in our layout, but now we need to hook it up to your activity. So add this to your onCreate method.
The last thing that is missing for our Toolbar is the styles. This consists of things like background color, popover color, height, button styles, etc. The next style is for v21 only. You’ll want use this in a styles-v21.xml file.
Setup the Navigation Drawer
First, we need to add in the drawer to our layout. So make your main activity’s layout look something like this:
We have the layout, but now we need to hook it up inside the activity. So we need to add in handling back button, configuration changes, and toggling. You main activity should start to look something like this:
With this, your navigation drawer will now but up to the bottom of your status bar. However, in the material design principles they make it clear they want a transparent status bar to draw OVER the drawer.
Making the Drawer full Application Height
If you are making a non 5.0 app this section isn’t required because it isn’t supported until v21
First you will need to set up a couple of properties in a special styles-v21.xml file so that we can force it to be full screen. You should only need windowDrawsSystemBarBackgrounds but I like to set a couple other transitions and stuff so set these:
With those styles, you just need to make sure the DrawerLayout in your main activity has the fitsSystemWindow attribute set to true. This should make the drawer go your status bar correctly. There is one problem though. Since the status bar is being drawn there android assumes you don’t want your content to start until under your status bar. So if you put an image on the top for the header (like Google does), you would get a white bar above it. So to fix this you need to add in a custom layout called ScrimInsetsFrameLayout which insets the view to get rid of that white bar. That class can be found in this gist:
Now just go and update the layout for your activity to use this layout to get a result somewhat like this:
and now you have a drawer that slides under the status bar exactly how the drawer is depicted in the material design guidelines!
When you change a title in a toolbar, it just flashes to a new value. I'm going to show you how to create an animation for transitioning between values.
Continue reading