2

Below is the code which I have for my application. Ive tried working on this but have not came across anything that works sucessfully for me. Without changing to fragments what is there that I can do.

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

        if (id == R.id.my_account) {

        } else if (id == R.id.nav_news) {

        } else if (id == R.id.nav_live) {

        } else if (id == R.id.nav_media) {

        } else if (id == R.id.nav_calendar) {

        } else if (id == R.id.nav_results) {

        } else if (id == R.id.nav_about) {

        } else if (id == R.id.nav_shop) {

        } else if (id == R.id.nav_social_media) {

        } else if (id == nav_fanzone) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
S.Doyle
  • 215
  • 3
  • 17

1 Answers1

0

change your onNavigationItemSelected() to this:

public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        switch(item.getItemId()){
             case R.id.my_account:
               //Do code here
              break;
             case R.id.nav_news:
               //Do code here
             break;
             case R.id.nav_live:
              //Do code here
             break;
             case R.id.nav_media:
              //Do code here
             break;
             case R.id.nav_calendar:
              //Do code here
             break;
             case R.id.nav_results:
              //Do code here
             break;
             case R.id.nav_about:
              //Do code here
             break;
             case R.id.nav_shop:
              //Do code here
             break;
             case R.id.nav_social_media:
              //Do code here
             break;
             case R.id.nav_fanzone:
              //Do code here
             break;

}

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

For change activities just add this inside all switch cases:

Intent intent=new Intent(your_activity.this,your_next_activity.class);
startActivity(intent);
Bruno Ferreira
  • 1,561
  • 1
  • 10
  • 17
  • Thanks for the answer Bruno. As im using navigation drawer, what would I place in the your_activity.this? thanks – S.Doyle Jul 24 '17 at 20:28
  • @A.Nutley if your navigation drawer is in MainActivity you just need to change your_Activity.this to MainActivity.this, your_Activity is the parent activity of navigation drawer. – Bruno Ferreira Jul 24 '17 at 20:33