How to create Menu item for NavigationDrawer programmatically

Hey,
If you need to create a menu item for your NavigationDrawer dynamically, then this is the best way to do it.
First we need to initialize our NavigationView, Menu and SubMenu objects :
// SubMenu Initialization
SubMenu subMenu;// NavigationView Initialization
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);// Menu Initialization
Menu menu = navigationView.getMenu();

How to add submenu and its items ?

subMenu = menu.addSubMenu(getString(R.string.subMenuTitle));
subMenu.add(0, Menu.FIRST, Menu.FIRST, getString(R.string.itemName))
        .setIcon(R.drawable.itemDrawable);
subMenu.add(1, Menu.FIRST + 1, Menu.FIRST, getString(R.string.itemName))
        .setIcon(R.drawable.itemDrawable);
As you can see first we create a submenu and set it a Title name. After that we can simple add a submenu item.

How to set that item onNavigationItemSelected? (Click Part)

int id = item.getItemId();if (id == subMenu.getItem(0).getItemId()) {
   // Magic Here
}
This code segment should be in “onNavigationItemSelected” part. Once you created a submenu, you can use it by getItem() and its item ID.

How to extract filename from Uri?

Now, we can extract filename with and without extension :) You will convert your bitmap to uri and get the real path of your file. Now w...