Adding Items To Navigation Drawer

To add items to navigation drawer, you need to edit two files. MainActivity.java and activity_main_drawer.xml

Adding Category to menu

Go to activity_main_drawer.xml and find the code mentioned below

activity_main_drawer.xml
    <group>
        <item
            android:id="@+id/nav_home"

            android:icon="@drawable/ic_home"
            android:title="Home" />
        <item
            android:id="@+id/nav_categories"
            android:icon="@drawable/ic_categories"
            android:title="Categories" />
        <item
            android:id="@+id/nav_offline"
            android:icon="@drawable/ic_file_download"
            android:title="Saved Posts"/>
    </group>

and paste the below code inside the <group> ... </group>

activity_main_drawer.xml
<item
            android:id="@+id/nav_my_category_id"
            android:icon="@drawable/ic_add"

            android:title="My Category Name"/>

Now the first part is completed. Lets proceed to the second step.

Go to MainActivity.java file and find the code mentioned below

MainActivity.java
@Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.nav_home){
            viewPager.setCurrentItem(0,true);
            bottomNavigation.setCurrentItem(0);
        }else if (id == R.id.nav_categories){
            viewPager.setCurrentItem(1,true);
            bottomNavigation.setCurrentItem(1);
        }else if(id==R.id.nav_offline){
            startActivity(new Intent(getApplicationContext(),OfflinePosts.class));
        }else if (id==R.id.nav_fb){
            openFbUrl("Google");
        }else if (id==R.id.nav_insta){
            openInstagram("Google");
        }else if (id==R.id.nav_tw){
            openTwitterUrl("Google");
        }else if (id==R.id.nav_yt){
            openYoutubeUrl("https://www.youtube.com/channel/UCNhT2txZHDfeIS0g7ZK7uRg");
        }else if (id==R.id.nav_web_url){
            openWebView("https://codecanyon.net/item/wordroid-full-wordpress-blog-app/19753667");
        }if (id == R.id.nav_about){
            openWebView("http://dev.itsanubhav.com/about-us/");
        }else if(id == R.id.nav_privacy){
            openWebView("http://dev.itsanubhav.com/privacy-policy/");
        }
        editor.apply();

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

You need to add following file to the onNavigationItemSelected just before the editor.apply();

MainActivity.java
else if (id == R.id.nav_my_category_id){
     openCategory("Category Name",121);
}

Full Code Example

MainActivity.java
@Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.nav_home){
            viewPager.setCurrentItem(0,true);
            bottomNavigation.setCurrentItem(0);
        }else if (id == R.id.nav_categories){
            viewPager.setCurrentItem(1,true);
            bottomNavigation.setCurrentItem(1);
        }else if(id==R.id.nav_offline){
            startActivity(new Intent(getApplicationContext(),OfflinePosts.class));
        }else if (id==R.id.nav_fb){
            openFbUrl("Google");
        }else if (id==R.id.nav_insta){
            openInstagram("Google");
        }else if (id==R.id.nav_tw){
            openTwitterUrl("Google");
        }else if (id==R.id.nav_yt){
            openYoutubeUrl("https://www.youtube.com/channel/UCNhT2txZHDfeIS0g7ZK7uRg");
        }else if (id==R.id.nav_web_url){
            openWebView("https://codecanyon.net/item/wordroid-full-wordpress-blog-app/19753667");
        }if (id == R.id.nav_about){
            openWebView("http://dev.itsanubhav.com/about-us/");
        }else if(id == R.id.nav_privacy){
            openWebView("http://dev.itsanubhav.com/privacy-policy/");
        }else if (id == R.id.nav_my_category_id){
            openCategory("Category Name",121);
        }
        editor.apply();

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

Last updated