Today I’m going to go over how to replicate the header you find at the top of the Google app drawers. If you haven’t already, you can check out the previous walkthrough on how to set up your drawer in the first place. It can be found here: Create Material Design Navigation Drawer
Dependencies
First thing you are going to need is to set up a layout that will be the header for the drawer. I personally use Piccaso to load the images. You can either use a custom transform to change the user image to a circle or you can use a circle image view. I chose the circle image view since it makes the default image circle as well. For both of these add these two dependencies to your application build.gradle:
The ‘+’ are where the version numbers go. You can leave it this way but android studio will throw a warning about them.
Layout Creation
Now I use the same layout for both 21 and previous so mine looks like this:
Insert Header into Drawer
The header then needs to be added into your drawer inside of your activity. This needs to be done BEFORE you attach an adapter to your listview.
Go to your activity with the drawer and find the onCreate method. I would recommend to make a private member variable for your header since we will be populating it after you have finished logging into the Google Account. We will then find and set the header for the drawer. (Now remember some of this code you probably recognize from the previous walkthrough so make sure it is in this order)
Once you add in you default profile picture, default banner picture, and an adapter (adapter must be set for the header to become visible) you can run this and you should see your header. Now all we have to do is hook up your Google account to it.
Populating Drawer with user’s Google Account
Now for the fun part. In your main activity where your drawer is going to be we need to set up a log in for the Google Api Client. On connection we will then pull the current person’s name, email, banner image, and profile image. What we need to do is set up some implementations for the callbacks (failed and success) and a couple more override classes. Eventually your main activity should look something like this:
Success
Now you should be able to run and see your new drawer header! Personal note I tend to like to make this the main activity and have fragments and controllers for navigation throughout everything so you only have to worry about logging in once and the drawer is always there (but that is just me).
Once you have an adapter added to this, your drawer should look something like this:
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 use the Android TV you might notice that when you focus on a card, the title area expands so you can read everything. However, if you use the ImageCardView this functionality does come baked in. So I will show you how to set up the OnFocusChangeListener to mimic this functionality.
Gist with Required Code
Feel free to adjust the amount of lines to display. This utilizes using a wrap content layout so it automatically expands when a layout parameter changes. If you really want to go a step farther and make it fancier I’m sure you could use a ValueAnimator to use an Interpolator to slide it open and have it use requestLayout(). If you do go that route you will need to render the content manually to get the bounds so you know how much to slide open.