How to Add a Navigation Drawer to All Activities For a Flavor

In this post i am going to describe how to add navigation drawer to all activities.However,  you may want to display navigation drawer in only one of build flavor (ex: pro version). Lets see how can we do this.

Firstly, we need a base activity that holds the navigation drawer. Then all activities that needs a navigation drawer will extend it.

Below is the BaseActivity:

As you see, this Base Activity doesnt call SetContentView in OnCreate. This is left to the Activity that extends BaseActivity. We override SetContentView to add the layout of the Activity that wants to show Navigation Drawer. We inflate the Activity layout with Parent Layout as Navigation Drawer. So in all activities that extends BaseActivity, Navigation Drawer becomes parent layout.

Below is the navigation drawer layout:

In the above layout file, FrameLayout becomes our activity content layout.

Suppose that you dont want to show Navigation Drawer in free version. Then you need to create a BaseActivity that directly inflates the Activity layout as below:

As you see the Navigation Drawer related codes are removed. You need to put this file to free flavor and previos BaseActivity to pro flavor.

References:

http://mateoj.com/2015/06/21/adding-toolbar-and-navigation-drawer-all-activities-android/

admin Hakkında

Murat Aydin works as a senior software engineer at Arcelik and develops Android apps. He has several Android applications in Google Play. He is a Sun Certified Java Developer and has eight years of experience in developing web based applications using Java technologies, and desktop and engineering applications using .Net technologies. Murat Aydin earned his BSc degree in Computer Engineering from METU (Middle East Technical University) and his MSc degree in Software Engineering from METU. He is a member of GDG Ankara (Google Developer Group Ankara, www.gdgankara.org), who organize several Android events in GDG Ankara, such as Android Developer Days. (www.androiddeveloperdays.com). twitter: @maydintr gmail: maydin