Fragment in Navigation Drawer Activity

Google has made it much more easier for developer to create an app with Navigation Drawer since Google’s I/O in 2015. In this tutorial, I will introduce how to switch among fragments when an item on navigation drawer is clicked.

  1. Create Drawer Navigation project
  2. Add blank fragment
  3. Add list fragment
  4. Add FrameLayout to MainActivity’s layout
  5. Switch Fragment on menu item click event

Create Drawer Navigation project

Start a new Android project with Navigation Drawer Activity. Fill in project info as in following image:

Out default Activity is

Add blank fragment

Let’s add a blank fragment named by clicking on package name > New > Fragment > Fragment (Blank). You don’t need to change any default settings generated by Android Studio.


Add list fragment

Similar to step above, add a list fragment named by clicking on package name > New > Fragment > Fragment (List). There are more info to fill in in this step because Android Studio auto-generate RecyclerView with its adapter. However, to keep it simple, let’s keep what Android Studio give us.


Add FrameLayout to MainActivity’s layout

Open content_main.xml and replace the default TextView widget with a FrameLayout. It is used to display fragment.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

        android:layout_height="match_parent" android:id="@+id/mainFrame" />

Switch fragment on menu item click event

There are several default menu items. I will use Import menu to display BlankFragment and Gallery one to display ListFragment.

This step requires modification only. Open and implement OnFragmentInteractionListener of 2 fragments first.

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, BlankFragment.OnFragmentInteractionListener, ItemFragment.OnListFragmentInteractionListener

Android Studio will show error. Click on the red bulb and choose “Implement methods”. The 2 methods are onFragmentInteraction(Uri uri)¬†and onListFragmentInteraction(DummyContent.DummyItem item).

If you want the app to show more details of an item in RecyclerView when it is clicked, input code within onListFragmentInteraction(DummyContent.DummyItem item) method.

    public void onFragmentInteraction(Uri uri) {


    public void onListFragmentInteraction(DummyContent.DummyItem item) {
        //Insert code here if you want to the app to do something when an item on RecyclerView is clicked. 
        //It is ofent used to show an Item's detail inforamtion. 

Finally, we need to modify onNavigationItemSelected(MenuItem item) method. Here is the full codes within the method:

    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        Fragment fragment = null;

        if (id == {
            fragment = new BlankFragment();
        } else if (id == {
            fragment  = new ListFragment();
        } else if (id == {

        } else if (id == {

        } else if (id == {

        } else if (id == {


        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(, fragment).commit();
        // Set action bar title

        DrawerLayout drawer = (DrawerLayout) findViewById(;
        return true;