XML : NavigationView, multiple menus

I'm trying to use the NavigationView in one of my projects to implement a side menu. The app has two type of users: Admin and User.

I created the login activity and I log in as user or admin. I pass the name, email via intent (putExtra) to my NavigationActivity.

Since I have two type of users I created two type of menus (admin has more menu options).

I check the content of the passed value from the intent and inflate the navigation views menu accordingly.

My problem is I don't know how to separate in the listener the two type of menus. Should I create in my if else two listeners for each case? What would be the right approach? The way I did it doesn't work correctly, in my side menu I have the first two items from the user menu and after that the reamining items of the admin menu.

Here's thecode:

    @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_navigation);          Bundle extras = getIntent().getExtras();          String name = extras.getString("name");          String email = extras.getString("email");          toolbar = (Toolbar) findViewById(R.id.tool_bar);          setSupportActionBar(toolbar);            navigationView = (NavigationView) findViewById(R.id.navigation_view);          if(name.equals("admin")){          // listener for admin here?              navigationView.inflateMenu(R.menu.admin_drawer);          }else{          // listener for user here?              navigationView.inflateMenu(R.menu.user_drawer);          }          navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {              // questionable part here, doesn't work correctly               @Override              public boolean onNavigationItemSelected(MenuItem item) {                  if(item.isChecked())                      item.setChecked(false);                  drawerLayout.closeDrawers();                  switch (item.getItemId()){                      // this is for user                      case R.id.test:                          Toast.makeText(getApplicationContext(), "Test Selected", Toast.LENGTH_SHORT).show();                          TestFragment testFragment = new TestFragment();                          android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();                          fragmentTransaction.replace(R.id.frameholder, testFragment);                          fragmentTransaction.commit();                          return true;                      // this is for user                      case R.id.practice:                          Toast.makeText(getApplicationContext(),"Practice Selected",Toast.LENGTH_SHORT).show();                          break;                      // this is for admin                        case R.id.existing_questions:                          Toast.makeText(getApplicationContext(),"Questions Selected",Toast.LENGTH_SHORT).show();                          break;                      // this is for admin                      case R.id.new_questions:                          Toast.makeText(getApplicationContext(),"New questions Selected",Toast.LENGTH_SHORT).show();                          break;                      // this is for admin                       case R.id.settings:                          Toast.makeText(getApplicationContext(),"Settings Selected",Toast.LENGTH_SHORT).show();                          break;                      // this is for admin                      case R.id.users_data:                          Toast.makeText(getApplicationContext(),"User data Selected",Toast.LENGTH_SHORT).show();                          break;                  }                  return true;              }          });    

Edit:

Here you have the two menus I use: admin_drawer.xml

   <?xml version="1.0" encoding="utf-8"?>  <menu xmlns:android="http://schemas.android.com/apk/res/android">      <group android:checkableBehavior="single">          <item              android:id="@+id/existing_questions"              android:checked="false"              android:icon="@drawable/ic_existq"              android:title="Questions" />          <item              android:id="@+id/new_questions"              android:checked="false"              android:icon="@drawable/ic_addq"              android:title="Add question" />          <item              android:id="@+id/settings"              android:checked="false"              android:icon="@drawable/ic_settings"              android:title="Settings" />          <item              android:id="@+id/users_data"              android:checked="false"              android:icon="@drawable/ic_statistics"              android:title="Statistics" />          </group>  </menu>    

user_drawer.xml

      <?xml version="1.0" encoding="utf-8"?>  <menu xmlns:android="http://schemas.android.com/apk/res/android">      <group android:checkableBehavior="single">          <item              android:id="@+id/test"              android:checked="false"              android:icon="@drawable/ic_test"              android:title="Test" />          <item              android:id="@+id/practice"              android:checked="false"              android:icon="@drawable/ic_practice"              android:title="Practice" />      </group>  </menu>    

No comments:

Post a Comment