Navigation Drawer
A sliding menu used to show important links in the Android Application is called Navigation Drawer. The navigation drawermakes it easier to navigate between the fragments. It is not displayed by default, so you must open it by sliding on the left or clicking the icon inthe taskbar.
In the broadest sense, the Navigation Drawer is an overlay panel, which takes the place of a task screen specifically dedicated to displaying all the options and links of an application.
Here, webuild a navigation drawer by utilizing the Drawer Layoutthat can be found in the Android support library. It displays three fragment links that can be navigated from a drawer.
Building a Navigation Drawer
1. Setup Drawer Resources
2. Create Fragments and Navigation Graph
Now, we need to create the fragments that will be displayed within the activity. It can be created within the application and become part of the activity. Be sure that all fragments extend androidx.fragment.app.Fragment.
Then build a navigation graph between the fragments as follows:
3. Setup Toolbar
For sliding the navigation drawer on the ActionBar, you should use the new toolbar widget present in the AndroidX library. Make sure that the toolbar slides above the taskbar with drawers that can be included in the view hierarchy.
Create a new layout file res/layout/nav_toolbar.xml and implement the given XML code:
4. Setup Header
It also accepts a custom attribute to view the layout that provides a header for the NavigationView layout.
To create a header, create a layout/nav_header.xml as follows:
5. Setup Drawer in Activity
Let's set the default navigation drawer according to the following layout file set in res/layout/activity_nav_draw.xml for the entire drawer. Remember adding the include tag for adding the toolbar as the first child of the main content view.
Now, let's setup the drawer in our NavDrawAcivity. We may also add menu icon.
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
importcom.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class NavDrawAcivityextends AppCompatActivity{
private AppBarConfigurationmAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav_draw_acivity);
Toolbar toolbar= findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Create your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.l_drawer);
NavigationViewnavigationView= findViewById(R.id.nav_view);
mAppBarConfiguration= new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer).build();
NavControllernavController= Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public booleanonCreateOptionsMenu(Menu menu) {
// Builds the menu; If present, adds items to the action bar.getMenuInflater().inflate(R.menu.nav_draw_acivity, menu);
return true;
}
@Override
public booleanonSupportNavigateUp() {
NavControllernavController= Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)|| super.onSupportNavigateUp();
}
}