Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change NavigationView items when user is logged

My app's main activity has a navigation drawer, instantiated in the XML in this way:

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/application_drawer"
    android:background="@color/white"/>

Now, the menu entry for the navigation drawer is the following:

<?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/login"
        android:icon="@drawable/ic_action_person"
        android:title="@string/login"/>
    <item
        android:id="@+id/settings"
        android:icon="@drawable/ic_action_settings"
        android:title="@string/settings"/>
    <item
        android:id="@+id/terms"
        android:icon="@drawable/ic_action_about"
        android:title="@string/terms_and_conditions_menu"/>
    <item
        android:id="@+id/about"
        android:icon="@drawable/ic_action_about"
        android:title="@string/info_hotelsclick"/>
</group>

What I'd like to do is to change the first item (and possibly the others as well) dynamically under some conditions. For instance, I'd like to change the "Login" entry with a "logout" one once the user has logged in ;-)

How can I achieve that? I managed to add an item to the Drawer in this way

    Menu menu = navigationView.getMenu();
    menu.add("Test");

but it doesn't sound that good to me, I'm pretty sure there must be a cleaner way.

...but does it?

like image 321
Marco Zanetti Avatar asked Jun 18 '15 14:06

Marco Zanetti


4 Answers

I thing the best approach to this is to include all your items in the menu and the change their visibility.

<item
    android:id="@+id/login"
    android:icon="@drawable/ic_action_person"
    android:title="@string/login"
    android:visible="true" />

<item
    android:id="@+id/logout"
    android:icon="@drawable/ic_action_person"
    android:title="@string/logout"
    android:visible="false" />

then

navigationView.getMenu().findItem(R.id.login).setVisible(false);
navigationView.getMenu().findItem(R.id.logout).setVisible(true);

You can also do this with whole groups of items

<group
    android:id="@+id/group_1"
    android:checkableBehavior="single"
    android:visible="false">
    ...
</group>

and

navigationView.getMenu().setGroupVisible(R.id.group_1, true)
like image 108
Lamorak Avatar answered Nov 15 '22 08:11

Lamorak


Simple solution:
Add two xml files in menu directory:

  1. navigation_with_login.xml Navigation Menu for logged in users

    <?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/nav_camera"
                android:icon="@drawable/ic_menu_camera"
                android:title="Import" />
            <item
                android:id="@+id/nav_gallery"
                android:icon="@drawable/ic_menu_gallery"
                android:title="Gallery" />
            <item
                android:id="@+id/nav_slideshow"
                android:icon="@drawable/ic_menu_slideshow"
                android:title="Slideshow" />
            <item
                android:id="@+id/nav_login"
                android:icon="@drawable/ic_menu_login"
                android:title="Login" />
        </group>
    
    
    </menu>
    
  2. navigation_with_logout.xml Navigation Menu for default user:

    <?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/nav_camera"
                android:icon="@drawable/ic_menu_camera"
                android:title="Import" />
            <item
                android:id="@+id/nav_gallery"
                android:icon="@drawable/ic_menu_gallery"
                android:title="Gallery" />
            <item
                android:id="@+id/nav_slideshow"
                android:icon="@drawable/ic_menu_slideshow"
                android:title="Slideshow" />
            <item
                android:id="@+id/nav_logout"
                android:icon="@drawable/ic_menu_logout"
                android:title="Logout" />
        </group>
    
    </menu>
    

Now you can change NavigationView items,just write few lines of code.

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);

if(islogin)
    {
        navigationView.getMenu().clear();
        navigationView.inflateMenu(R.menu.navigation_with_login);
    } else
    {
        navigationView.getMenu().clear();
        navigationView.inflateMenu(R.menu.navigation_with_logout);
    }
like image 31
manish jain Avatar answered Nov 15 '22 09:11

manish jain


Frst get the navigationmenu

 NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
Menu menu = navigationView.getMenu();

To add the menu dynamically

 if(loggedOut){
        menu.add(R.id.submenu_others, R.id.action_logout, Menu.NONE, "logout");
}

Here is menu.add(groupId, menuItemId, orderOfMenu, menuItem text)

     if(loggedIn){
        menu.removeItem(R.id.action_logout);
}
like image 3
yubaraj poudel Avatar answered Nov 15 '22 09:11

yubaraj poudel


I got it done, try this when you need to change the title:

navigationView.getMenu().findItem(R.id.yourItemId).setTitle("my title");

Hope it helped!

like image 2
Harry's Lab Avatar answered Nov 15 '22 07:11

Harry's Lab