Adding a Toolbar and Navigation Drawer Across all Activities of an Android App21 Jun 2015
Gone are the days of using the sucky actionbar. With the introduction of Android 5.0, the actionbar has been replaced by the toolbar. The Toolbar allows more customization and can be placed anywhere in the UI since it’s just a viewgroup. In order to use the toolbar as an actionbar, you must declare it in your xml and set it as the support action bar in the activity.
But what if you want the toolbar to be constant throughout your app without having to declare it in each one of your activities? In this tutorial, I will show you how to set up the toolbar for all your activities and as an added bonus, we will setup a navigation drawer as well.
Setting up the project
Create a new android project using your preferred method. I will be using Android studio and Gradle for this tutorial and I suggest you do as well.
I’m setting the API level to 16 but you can set yours to any version you like. Since we will be using the support library, you can go back as far as API level 7.
Open the build.gradle file for the main module of your project and add the appcompat v7 support library. Usually, this is under a folder called “app” or “mobile.” The support library might already be in your build.gradle, but if it’s not, add it under dependencies:
Let’s also add the support library v4 and the design support library which we will use to set up the navigation drawer:
If you didn’t create an activity when you created your project, go ahead and create a new blank activity.
Setting up the Toolbar
Open the styles.xml file that should be under
res/values and change its content from:
This will disable the old actionbar so that we can add our custom toolbar instead.
Create a new activity called BaseActivity and a corresponding layout called activity_base.xml.
Open the activity_base.xml and replace its content with the following code:
It’s time to set up our base activity. We will override the setContentView method so we can hijack the view and attach it to our container instead of the android default container.
Open BaseActivity.java and make sure to remove
setContentView(R.layout.activity_base) from the
Now let’s override the setContentView() method with our own implementation:
Open your main activity and make sure it extends BaseActivity:
Compile and run your app. It should now have a toolbar and look similar to this:
You can also swipe from the left to open the navigation drawer.
Allowing activities to opt-out of having a toolbar
What if you have an activity that doesn’t need a toolbar? Let’s change our base activity to allow its children to not display a toolbar.
Add a new method to BaseActivity called useToolbar that returns true by default:
Modify the setContentView method of the base activity as follows:
Any activity that wants to opt out from having a toolbar just needs to override the useToolbar method and return false.
One of the things to consider when using this approach is that every activity inflates a toolbar. During your activity transitions, the toolbar will animate with your activity. You can get around that by using Shared Element Transitions