0
For now I’m setting the menu through xml, this way:
<android.support.design.widget.NavigationView
android:id="@+id/navView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/colorBackgroud"
app:headerLayout="@layout/nav_header"
app:itemIconTint="@color/textColorPrimary"
app:itemTextColor="@color/textColorPrimary"
app:menu="@menu/menu_nav">
However I want before setting the menu to do a check with if, for this I would need to set it by code, for example... if(if) user was equal to "administrator" open a menu other than "users".
follows my code for the time being:
NavigationView navigationView = (NavigationView) findViewById(R.id.navView);
navigationView.setNavigationItemSelectedListener(this);
How do I set the navigationview menu by code?
Are you creating one from scratch or using the Layout generated by Android Studio itself? I suggest you create a new Activity of type Navigation Menu. Automatically the code will be generated and the Layout that calls this menu. Thus, you would make a second Layout and validate the user, depending on its validation inflates the user layout if it does not inflate the Admin layout.
– Gean Miguel
I’ve seen some ways to do this without having to make a layout, and actually this is exactly what I’m looking for, as I do to set only the different menu and not the layout.
– Luis Felipe
Even if you can create using the same, I believe it would not be worth it because it would have to validate also the fields that will appear for one and another.
– Gean Miguel
I thought of only changing which menu file I will use depending on the user, for example. If you were an administrator - menu_nav_adm.xml and if you were a normal user - menu_nav_usuarios.xml
– Luis Felipe
Exactly, that’s what I tried to say in the first comment, I ended up expressing myself badly hehe, but the idea is this same.
– Gean Miguel
I managed to do here, I went to take a look at the doc and got it. I used the if normally and if it was true it inflated a type of menu. Code used: navigationView.getMenu(). clear(); navigationView.inflateMenu(R.menu.menu_nav);
– Luis Felipe
If you want to write an answer so I can accept it, you can do it!
– Luis Felipe
Show, puts the excerpt of the solution as answer, becomes clearer and objective in this way. =)
– Gean Miguel