Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Navigation Drawer to all the activities in the application?

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 ?

like image 553
Vipul J Avatar asked Sep 19 '13 10:09

Vipul J


People also ask

How do I show navigation drawer in all activities?

Navigation drawers are the most common use android widget in android. The user can view the navigation drawer when they swipe the activity's screen from the left edge of the android device. A user can also find it from the activity, by tapping the app icon (also known as the “hamburger” menu) in the action bar.

How do I add a navigation drawer?

To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.

Which view is used to add the navigation drawer to an app?

Step 3 -Setup NavigationView NavigationView is an easy way to display a navigation menu from a menu resource. This is most commonly used in conjunction with DrawerLayout to implement Material navigation drawers.

Can we use activity in navigation drawer?

As for how to switch between activities via the navigation drawer, you can just set up new intents within your selectItem() method: private void selectItem(int position) { // Handle Navigation Options Intent intent; switch (position) { case 0: intent = new Intent(currentActivity, NewActivity. class); intent.


1 Answers

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.

like image 117
Steve Benett Avatar answered Sep 25 '22 05:09

Steve Benett