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 ?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With