Whats the efficient way to add Navigation Drawer on all the activities? I don't want to repeat the code for Navigation Drawer in all the activities and their layouts. Is it possible somehow to add Nav. Drawer in BaseActivity(custom class) and then every other activity will extend BaseActivity inorder to have the Navigation Drawer ?
Asked
Active
Viewed 1.6k times
7
-
Why not using one BaseActivity and Fragments? – hardartcore Sep 19 '13 at 10:35
-
1Because I don't want to use fragments everywhere in my application, thats not efficient. – Vipul J Sep 19 '13 at 10:38
-
Why do you think that using Fragments is not efficient? – hardartcore Sep 19 '13 at 10:41
-
It can be much easier: http://stackoverflow.com/questions/4922641/sliding-drawer-appear-in-all-activities – Shayan_Aryan Jun 05 '14 at 22:09
-
if your question solved,plz post an example ! this question : viewed 4043 times !!!! – S.M_Emamian Sep 10 '14 at 19:58
-
http://stackoverflow.com/questions/32197791/how-to-access-searchview-in-navigation-drawer – Aditya Vyas-Lakhan Aug 25 '15 at 07:59
-
@AdityaVyas-Lakhan, page is not found. – CoolMind Nov 30 '16 at 10:46
1 Answers
13
Is it possible somehow to add Nav. Drawer in BaseActivity(custom class) and then every other activity will extend BaseActivity inorder to have the Navigation Drawer ?
Yes this is definitly the cleanest way to go.
public BaseActivity extends Activity {
@Override
protected void onCreate()
super.onCreate(); // calls Activity.onCreate()
// setup your Navigation Drawer
}
public FirstActivity extends BaseActivity {
@Override
protected void onCreate()
super.onCreate(); // will call the BaseActivitiy.onCreate()
// do something in the FirstActivity
}
public SecondActivity extends BaseActivity {
@Override
protected void onCreate()
super.onCreate(); // will call the BaseActivitiy.onCreate()
// do something in the SecondActivity
}
The "hard work" will be the layouts. Have one baseLayout for the BaseActivity with a place holder for the Content View (the visible part of the Activities). For all other Activitys use this layout and include your Content View.

Steve Benett
- 12,843
- 7
- 59
- 79
-
So do I have to call setContentView(drawerLayout) in BaseActivity as well ? – Vipul J Sep 19 '13 at 10:36
-
Yes, otherwise it makes no sense. The BaseActivity is used for the Drawer, that all Activitys have the same one. Have seperated layouts for the BaseActivity and all others. – Steve Benett Sep 19 '13 at 10:40
-
1Okay Thanks. Will try this. I thought this way but was not sure that how will it behave to have two layouts for an activity.(One from activity itself and one from BaseActivity) – Vipul J Sep 19 '13 at 10:47
-
Steve : I tried this. After doing it I can see the navigation drawer icon in action bar but can't interact with it. Any idea why ? Thanks again. – Vipul J Sep 19 '13 at 10:56
-
1@VipulJ Have a look [here](http://stackoverflow.com/questions/4922641/sliding-drawer-appear-in-all-activities). – Steve Benett Sep 19 '13 at 12:52
-
@SteveBenett I tried this. After doing it I can see the navigation drawer icon in action bar but can't interact with it. Any idea why ? I have also looked here(http://stackoverflow.com/questions/4922641/sliding-drawer-appear-in-all-activities), as you said in above comment, but nothing helped. – Shridutt Kothari Feb 16 '14 at 17:25
-
@VipulJ did you able to interact with navigation drawer icon in action bar??? How??? please reply? i am wasting my time on this from last one day. – Shridutt Kothari Feb 16 '14 at 17:27
-
@shriduttkothari Can you open a new question for your specific problem with some details? – Steve Benett Feb 20 '14 at 08:14
-
@VipulJ Did you get this working? I am having the same question at the moment. Particularly about how to use two layouts for an activity (one for the nav drawer in BaseActivity, and one for the other child activity). – theblang Mar 07 '14 at 22:17
-