Android Navigation Drawer Example using Fragments

Hello guys, here is another tutorial for one of the most common things in any android application, navigation drawer. So in this Android Navigation Drawer Example you will learn how you can use the Android Navigation Drawer from the predefined template.

You may already know what is Android Navigation Drawer but if you are confused in implementing it with multiple fragments then don’t worry, this android navigation drawer example will clear all your doubts. So lets begin our Android Navigation Drawer Example.

Android Navigation Drawer Example Video

  • You can also watch this complete video tutorial if you are having problem understanding the written tutorial.

Android Navigation Drawer Example

Creating a new project

  • So first we will create a new project. I am using Android Studio. (No reason of using eclipse nowdays 😛 )
  • Create a new project. I created NavigationDrawerExample.
  • Now when you are asked to select an activity from the template, select the navigation drawer activity.
android navigation drawer example
Navigation Drawer Activity

Adding Navigation Drawer Activity

  • If you want to add Navigation Drawer Activity to your existing project you can simply add it by going to your package -> right click -> new -> activity -> navigation drawer activity  
android navigation drawer activity
Adding Android Navigation Drawer Activity
  • Once your project is loaded your navigation drawer activity is ready. You can now run your application. 😛
android navigation drawer example
Android Navigation Drawer Example
  • Pretty easy right? Yeah its quite easy. Now we will learn to customise the menus and to add the screens for the menus using fragment.

Customising Navigation Drawer Menus

  • Now you can change the menus given in the drawer to whatever you want. To change the menu open the activity_main_drawer.xml inside menu folder.  Here is my activity_main_drawer.xml file I deleted the menus and now we have only 3 menus (You can add as many as you want).

  • I changed the text to Menu 1, Menu 2 and Menu3 and for icons I used the default android icon. You can use custom icons just paste the icon image inside drawer folder and you can use them.
  • If you try to run app now it will give error, as we have changed the menu items and ids. So first go inside (or the java file for your navigation drawer activity). And modify the overriden method onNavigationItemSelected(MenuItem item) as follows.

  • Here we removed the previous if else conditions and added our own according to the customised menu ids.
  • You can also customise the navigation drawer header. For this you need to go to the nav_header_main.xml file. I also changed this file as below.

  • Now you can run your application.
android navigation drawer example
Android Navigation Drawer Example

Removing the Floating Action Button

  • You can see a circular red button in your activity. You can remove it if you do not need it. To remove this button go to app_bar_main.xml inside the layout folder and remove the floating button from there.

  • To remove the default Hello World text view go to the content_main.xml file.
  • As we have removed the button we also need to modify the code inside onCreate() method of so modify it as follow or you will get error.


  • Now we will create fragments for our navigation menus.

Creating Screens for Navigation Menus using Fragment

Now whenever we click on a navigation item from the drawer a respective screen should open, for this we will use fragments. As we have three navigation menus we will create three layouts inside our layout folder.

  • Right click on layouts folder and create a new layout resource file. I named it fragment_menu_1.xml, fragment_menu_2.xml and fragment_menu_3.xml. Write the following xml code inside all these files.

  • All the three files contains the same code only for the text I changed it to Menu 1, Menu 2 and Menu3 for the respective files.
  • You can design the screens according to your application requirement but for now I am justing putting a normal TextView as it is an example demonstrating the concept only.
  • So we have the layouts now we will put these screens inside your activity using fragments. For this we need a FrameLayout so go inside content_main.xml file and add a FrameLayout.

  • Now we will create the Fragments.
  • So create 3 java classes inside your package named Menu1, Menu2 and Menu3 and write the following code.

  • For the other two classes code will be the same, you only need to change R.layout.fragment_menu_1 with the respective layout file for your fragment.

Switching Between Fragments on Navigation Drawer Menu Selection

  • Now come inside and empty the method onNavigationItemSelected(MenuItem item) as follows.

  • Now here we will create one more method named displaySelectedScreen(int itemId).

  • We will call this method whenever a navigation menu item is selected. So inside onNavigationItemSelected(MenuItem item) we need to add the following line.

  • Now when the activity is first loaded we will display Menu1. For this just add the following line inside onCreate() method of

  • So the final code for is.

  • Thats it now just run your application.
android navigation drawer example
Android Navigation Drawer Example
  • Bingo! Its working absolutely fine. If you are having troubles you can get the source code from the below github repository.

Android Navigation Drawer Example Source Code

So thats it for this Android Navigation Drawer Example friends. Feel free to leave your queries by comments. And please do share this post in your social networks if you found it useful. Thank You 🙂

54 thoughts on “Android Navigation Drawer Example using Fragments”

  1. Hi i’m having rendering problems on my activity_main_drawer.xml
    Exception raised during rendering: (Details)
    and nothing shows. Pls how can i fix this problem

  2. There is an issue of selected item color if adds child items under parent menu like menu -> menu1 -> child1, child2. Doesn’t highlight selected child.

  3. Thanks dude.

    From all the tutorials out there, your tuto is the most straight to the point and easy to understand !

    I like the way you arrange the steps to follows with the screenshots.
    Thanks dude!

  4. if (fragment != null) {
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(, fragment);

    I am getting an error. wrong second argument type.Required,fragment


    • Check your imports you may be using only Fragments but you have to use import;
      So check the import line for fragment and replace that with import;

  5. Thanks for this. It’s working 🙂

    Question, is it possible to make the ActionBar (or ToolBar) as Search View.
    I add Search in menu part ( onCreateOptionsMenu) but kept on getting null value in SearchView part

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(, menu);

    MenuItem searchItem = menu.findItem(;

    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);

  6. Thanks. It’s working as a separate app. But, when integrated into another project, it doesn’t work. No errors are shown, just the app crashes in the emulator. What can be the error here?

  7. Great work, but I have a problem I would like to add a webview in one of the xml fragments. I added the internet permissions to the manifest, added the code to the in the xml fragment inserted the webview I tried everything but I did not succeed. Can you help me? Thank you.

  8. thanks …Very nice tutorial but make sure android studio is updated or a newer version almost 2.3.2…
    i have got so many problem while implementing in an old version..

  9. while creating new java class. superclass & interface is required. what to fill in theses columns. jus take the example of menu1 java class.

  10. Hi

    After having gone through many downloads and blogs, I finally got the best. Thanks for the great, lucid video.

    Thanks again


  11. i am trying to display the four fragments but when we clicking on the other fragments…the same fragment is displaying and the other fragments are not displaying……..please help on this..

  12. HI, I did the same code, with my fragment layout, but I got this error
    java.lang.illegalArgumentException: No view found for id 0x7f0d0086 (…:id/content_frame) for FragmentHome

  13. the best tutorial about navigation drawers i seen so far. very clear and precise
    suitable for beginners. can get your app working in seconds

  14. Hi I have added the navigation activity to existing project and customized as per my need When it is executed ,I got the message as unfortunately app has stopped…does this method be possible for adding navigation activity to existing project and does this navigation activity can be used as second or next activity?? please give me the solution.

  15. is there no need to implement listener interface as we do in Fragment communication? i.e a listener interface is created in Fragment and that interface is implemented in MainActivity and then a callback method is used …

    • Hi Shoaib,
      that’s correct. based on what i understood. listener interface is there in order to allow events to pass back from Fragment to parent activity and it can be used for communication between one Fragment to another. if your app does not have such a need, you can skip it.

  16. Thanks for sharing the knowledge. It was really useful.
    One small comment from my side as it took me sometime to figure out.
    If you need to preserve the last fragment when we click on back button we should add the following:

  17. Like import I have created home page.In home page I hve to write some description about my content with url address of to visit website for more content. Sir then plz me snd description how to implemente it

  18. Thanks for your tutorial it’s very easygoing
    I’m facing a problem
    fragment = new ImportFragment();

    it showing error Please tell me the reason

    • Make sure you have imported the correct Fragment, there are two Fragment classes, one is another one is or something like this, you have to choose the second one.

  19. Dear, I need help. I have 2 buttons in fragment menu 1,
    menu2 and menue3
    now i want on click of button menu2 open fragment of menu2
    and on click of button menu3 open fragment of menu3

  20. Holy shit, thank you! I’ve been trying to work out how to make a working navigation thing (the nav codelab was using Kotlin and intents wasn’t working out, I also tried using fragments but they didn’t work as intended) Thank you so much, I’ve been stuck on this for 2 weeks now!


Leave a Comment