A navigation drawer is a panel that usually displays navigation options on the left edge of the screen, as shown on the right side of the figure below. It is hidden most of the time, but is revealed when the user swipes a finger from the left edge of the screen or touches the navigation icon in the app bar
- A navigation drawer as the Activity layout root ViewGroup.
- A navigation View for the drawer itself.
- An app bar layout that will include a navigation icon button.
- A content layout for the Activity that displays the navigation drawer.
- A layout for the navigation drawer header.
- Populate the navigation drawer menu with item titles and icons.
- Set up the navigation drawer and item listeners in the activity code.
- Handle the navigation menu item selections.
To add a navigation drawer, use a DrawerLayout as the root view of your Activity layout. Inside the DrawerLayout, add one View that contains the main content for the screen (your primary layout when the drawer is hidden) and another View, typically a NavigationView, that contains the contents of the navigation drawer.
- we added the DrawerLayout as the root of the activity.
- we added a NavigationView to the DrawerLayout.
Note: the main content of the activity is the same and we only moved the content and it's container to the DrawerLAyout.
- we created a nav_header_main Layout for the header of the navigation view and.
- we created a activity_main_drawer menu resource and added the menu items with icon inside it. (check out the menu resources)
- here is the ml code of the navigation view:
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />- we added a toolbar and inflate is as the app bar of the activity.
//Inflate The Toolbar:
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);- to added to toggle button to open and close the drawer we used the following code inside the MainActivity:
//inflating the Navigation View
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
if (drawer != null) {
drawer.addDrawerListener(toggle);
}
toggle.syncState();- we defined the navigation view inside the main activity and added a item click listener for it.
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected (@NonNull MenuItem item) {
Log.d(TAG, "onNavigationItemSelected: " + item.getItemId());
drawer.closeDrawer(GravityCompat.START);
return false;
}
});- we added the following code to check if the drawer is open and back button is clicked, the drawer should close
/**
* Handles the Back button: closes the nav drawer.
*/
@Override
public void onBackPressed () {
if (drawer!=null && drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}- we created a gradient background for the header of the drawer. to do so , we created a DrawableResourceFile in the drawable folder and added to folloing code inside it:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="135"
android:centerColor="#4CAF50"
android:endColor="#2E7D32"
android:startColor="#81C784"
android:type="linear" />
</shape>