How to Use Navigation Drawer In Android Studio Java?

In this example we will learn how to create a navigation drawer in android studio. Mainly it opens from the left or right side when we slide our finger in android mobiles . It is also called sidebar in android studio. This is also called android drawerlayout.

navigation drawer in android
navigation drawer in android

What is navigation drawer in android studio?

Navigation Drawer is part of the screen that contains some data like header and menu items in android. It opens when we click on the toggle button. Usually the toggle button is placed on the toolbar. This also opens when we slide the android device screen from the sidebar as made from left or right.

navigation drawer in android studio
navigation drawer in android studio

See the above image, this is an example of a navigation drawer. As i told you it contains some data like header layout and menu items. When we click on any item it will load or redirect to another screen. We can also move to another android screen by clicking on its header layout.

Why do we use the navigation drawer in android?

When we want to load multiple screens at one place and can go to any screen from one place. Then we use navigation drawer in android. The drawer and its items remain the same when we click on its navigation menu items. The desired screen is loaded only and it replaces the previous screen.

How to create a navigation drawer in android studio?

To create a drawerlayout in android, you have to create the following screens and classes. We needed following components :-

  1. DrawerLayout
  2. Toolbar
  3. FrameLayout
  4. Header Layout
  5. Navigation Items
  6. NavigationView

Now we will discuss one by one in brief.

Enable DataBinding in android studio

Please note that I used DataBinding in this project to fetch ids. So first please enable dataBinding. Click here to enable dataBinding in android studio.

1 DrawerLayout

It is the parent widget of the screen in which we want to add the drawer. It contains Toolbar, FrameLayout and NavigationView. We have to create all these three item inside the drawerlayout.

To create navigation drawer in android, we are using androidx library. Material design also provides drawerlayout. There is no big difference between these two. Use following component to create drawerLayout-

Don’t forget to add the following line in this drawerLayout.


2. Toolbar

Toolbar is fixed at the top of the screen. Navigation drawer toggle in android takes place on it. It can be left side or right side according to us, we are creating it from the left side. By clicking on the toggle icon, the drawer will open from the left side.

To use the toolbar height use the following component in the toolbar.


3. FrameLayout

FrameLayout in android is used to load multiple screens in a single place. Generally it loads fragments instead of activity. So we have to create the screen of the fragment type, which we want to load in this layout by clicking on navigation drawer items.

To create FrameLayout use the following code in your project.

4. Header Layout

Header layout is not any specific layout, this is just a simple layout which is shown in above image. Generally it contains a circular image, name and email id. So this is custom you can create it according to you and set the data.

You can create it in app – > res -> layout. And mouse right click – > new -> Layout Resource File  and give a name as you want. After creating this layout copy and paste the following code-

5. Navigation Items

To show the items in the navigation drawer we have to create a list of items. With icons and text. These items will be listed below the header layout in drawerLayout in android.

First of all create the menu directory in res using the way – res -> right click -> new – > Android Resource Directory and name it menu.

After that right click on menu directory – > new -> Menu Resource File -> give a name -> Choose menu as Directory name from dropdown. And paste the following code.

I created only three items you can create here as much as you want. And add the icon from the drawable folder.

6. NavigationView

NavigationView holds the navigation drawer elements like headerLayout and navigation items. We can manage it according to us by giving the padding, margin and other styles. Use the following code for NavigationView –

Complete Example of Navigation Drawer in android

I combined the above whole code and created an example. You can directly use following code in your project –

activity_main.xml

This is the main layout xml file which contains DrawerLayout, Toolbar, FrameLayout and NavigationView. Cope and paste it to use.

MainActivity.java

For back pressed i created the OnBackPressed interface in MainActivity. When we are in the last Fragment like in About Us Fragment. If we press back button then we will redirect to the home Fragment like in HomeFragment().

I also changed the color of the selected item in the navigation drawer of android. Like if i press on Contact Us item then its background color will be changed. So this feature is already implemented in this.

To load the fragment in frameLayout in navigation drawer in android, FragmentManager and FragmentTransaction are used to call the next drawer item. So I created all these functions inside a CallFragment()  and passed the fragment inside it. And this method is calling from onNavigationItemSelectedListener. I used switch() to call different fragments by using id from the menu item.

nav_header_layout.xml

Add the following dependency for CircleImageView.

This layout file contains the headerLayout data. Use your icons or photos from the drawable folder in CircleImageView as src.

navigation_item.xml for navigation drawer

These are navigation items which will be listed below the header in the navigation drawer. Change the icons from your icon from the drawable folder. You can expand this list as you need.

item_background_nav_checked

Create a Drawable Resource file in drawable folder and give the name – > item_background_nav_checked

This file is used to change the color of selected navigation drawer items. When we click on any item it will show as selected and its background color will be changed, so we understand that this item is selected.

Fragments For Navigation Drawer in Android

Here we created three navigation items, So we needed to create three fragments to show the data. These are different from activities. I created three fragments. You can create these as follows –

Java -> first package – >  Right click -> New – > Fragment – > Fragment ( Blank )

And copy paste the following code for all these three fragments. After Copy and paste you can manage the layout as you need. 

HomeFragment.java

All these three fragments contain TextView only.

fragment_home.xml

ContactUsFragment.java

fragment_contact_us.xml

AboutUsFragment.java

fragment_about_us.xml

Calling HomeFragment() on backpress in Navigation drawer android

As I created an interface in MainActivity(). This interface is implemented in all fragments and overrides its method. These are some basic feature of navigation drawer in android.

In the override method we loaded HomeFragment() by replacing the old fragment from the frameLayout.

Conclusion Of Navigation Drawer In Android Example

Finally we successfully created the Navigation drawer in android studio. All type features are implemented in it.

If there are any problems in this tutorial please comment as well as if this is helpful to you.

See more android and flutter examples. See also More . Learn databinding in android.


Don’t miss new tips!

We don’t spam! Read our [link]privacy policy[/link] for more info.

Leave a Comment

Scroll to Top