title: How to add menu and submenu in NavigationView dynamic in android
body: <p>I want to add custom menu in slide navigation with menu icon as well as sub menu dynamically.Below code add menu successfully but sub menu is showing when slide navigation appear as well as unable to add icon to the menu so please help me to hide sub menu and show only menu with icon and when I click on menu then only its sub menu should be appear.</p>
  
  <pre><code>    public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
  
      private Toolbar toolbar;
      private DrawerLayout drawer;
      private NavigationView navigationView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          getId();
  
          ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                  this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
          drawer.setDrawerListener(toggle);
          toggle.syncState();
  
  
          navigationView.setNavigationItemSelectedListener(this);
  
          // Add menu dynamically
          Menu m = navigationView.getMenu();
          SubMenu topChannelMenu = m.addSubMenu("Top Channels");
  
          // Add submenu.
          topChannelMenu.add("Foo");
          topChannelMenu.add("Bar");
          topChannelMenu.add("Baz");
  
          topChannelMenu = m.addSubMenu("Top Channels2");
          topChannelMenu.add("Foo2");
          topChannelMenu.add("Bar2");
          topChannelMenu.add("Baz2");
  
  
          MenuItem mi = m.getItem(m.size()-1);
          mi.setTitle(mi.getTitle());
      }
  
      private void getId()
      {
          toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          navigationView = (NavigationView) findViewById(R.id.nav_view);
          drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
      }
  
      @Override
      public void onBackPressed()
      {
          DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
          if (drawer.isDrawerOpen(GravityCompat.START))
          {
              drawer.closeDrawer(GravityCompat.START);
          } else
          {
              super.onBackPressed();
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu)
      {
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item)
      {
          int id = item.getItemId();
  
          if (id == R.id.action_settings)
          {
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  
      @SuppressWarnings("StatementWithEmptyBody")
      @Override
      public boolean onNavigationItemSelected(MenuItem item)
      {
          // Handle navigation view item clicks here.
          int id = item.getItemId();
  
          if (id == R.id.nav_camera)
          {
              // Handle the camera action
          } else if (id == R.id.nav_gallery)
          {
  
          } else if (id == R.id.nav_slideshow)
          {
  
          } else if (id == R.id.nav_manage)
          {
  
          } else if (id == R.id.nav_share)
          {
  
          } else if (id == R.id.nav_send)
          {
  
          }
  
          drawer.closeDrawer(GravityCompat.START);
          return true;
      }
  </code></pre>