How to setup navigation drawer in android application

Date : 29/05/2019


With the navigation drawer one can navigate to many screens or functionalities of the app by clicking on the ‘hamburger’ icon. Swiping from the left is also a way to bring the drawer into view, a screen then slides in, showing many items. You can click on these said items and go to those screens to use that feature of the app.

Step 1 : Create a new project

As the blog only describes the setup of navigation drawer, create a fresh project. Navigation drawer can be demonstrated in existing project too.

Primarily we need a header XML file and XML file under menu category and Navigation drawer implementation in activity’s or fragment’s XML file.

Step 2 : Create header and menu categories for Navigation drawer

Create an XML file that include Image and other views withing image view if you want to include

Create XML file that this only to manage options in the navigation drawer.This should only under menu category.

Step 3 : Setup menu and header with navigation drawer in Activity’ XML file

You have to setup Navigation widget inside Drawer widget in XML file. Most common use cases will have layout_width not as match parent but to a certain limit. Next need to include header file and menu file inside Navigation object.

Step 4 : Essential code for navigation drawer demonstration

You need to navigation drawer and NavgationView object to manipulate the views in activity class. You get it from findViewById attribute in activity class. After getting drawer object, Create a hamburger in action bar and sync it. Then put listener for NavigationView.

Step 5 : Implement NaigationView.OnNavigationItemSelectedListener

Inherit NaigationView.OnNavigationItemSelectedListene to implement methods upon clicking the different views in the drawer. New to override those all methods in in your activity.

As the name suggests, you can to whatever with the menu item object. In this code, it will move to another fragment upon clicking the options to different fragments

We need to manually operate drawer which means it should be closed if it is opened.

Thanks for using pheonix solutions.
You find this tutorial helpful? Share with your friends to keep it alive.

Leave a Reply