Reputation: 474
I am developping an application which asks you to log in, and start an activity depending on your account. The problem is that I have a drawer layout, and I need to change the menu.xml file depending of the activity. I know that it is possible to use conditional structures in an XML file. Is there any way to retrieve the name of the calling Activity in the XML file? That would allow me to load a different menu depending of the name of the activity calling the XML file.
This is the XML file calling the menu_main.xml file :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
style="@style/nav_view"
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemTextColor="#FFFFFF"
app:menu="@menu/menu_main"/>
And the XML file :
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:checkableBehavior="single"
>
<item
android:id="@+id/nav_profile"
android:checked="true"
android:title="@string/nav_profile"/>
<item
android:id="@+id/nav_search"
android:title="@string/nav_organisation"/>
<item
android:id="@+id/nav_logout"
android:title="@string/nav_logout"
/>
</group>
Thanks in advance !
Upvotes: 0
Views: 36
Reputation: 906
Do you really want to do such a thing inside a xml? You can set menu dynamically prom java code:
navigationView.getMenu().clear(); //clear old items
navigationView.inflateMenu(R.menu.new_navigation_drawer_items); //inflate new menu
or
Menu menu = navigationView.getMenu(); //get current menu
menu.add();//add an item
Upvotes: 1