How to generate Navigation Drawer item's dynamically from xml?

KittoKatto

I prepared 2 xml menu files and placed inside my res/menu folder.

menu_beforeLogin.xml will be displayed if the user didn't logged in. After they logged in, the menu item will changed to menu_afterLogin.xml.

I tried to called InflateMenu() in my onResume() method, but it ends up with keep adding the items from xml file whenever onResume() is invoked instead of removing/regenerate the menu list.

I would prefer to check user is logged in or not in onResume() method.

What is the proper way to change Navigation Drawer's list dynamically ?

MainActivity.cs

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);
    ...
    mDrawer = FindViewById<NavigationView>(Resource.Id.navigation_drawer);
    ...
}

protected override void OnResume ()
{
    base.OnResume ();
    string token = pref.GetString (Constant.PREF_TOKEN, "");
    mDrawer.InflateMenu ( (token == "") ? Resource.Menu.menu_drawer_notLoggedIn : Resource.Menu.menu_drawer);           
    mDrawer.Invalidate ();
    ...
}

layout/main.axml

<android.support.v4.widget.DrawerLayout>
    <android.support.design.widget.CoordinatorLayout>
    ...
    </android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@drawable/menu_background"
        app:headerLayout="@layout/drawer_header" />
</android.support.v4.widget.DrawerLayout>

res/menu/menu_beforeLogin.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_login"
        android:icon="@drawable/navigation_login"
        android:title="@string/navigation_login" />
    <item
        android:id="@+id/navigation_notice"
        android:icon="@drawable/navigation_notice"
        android:title="@string/navigation_notice" />
    <item
        android:id="@+id/navigation_contactUs"
        android:icon="@drawable/navigation_contactUs"
        android:title="@string/navigation_contactUs" />
</menu>

res/menu/menu_afterLogin.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_notice"
        android:icon="@drawable/navigation_notice"
        android:title="@string/navigation_notice" />
    <item
        android:id="@+id/navigation_profile"
        android:icon="@drawable/navigation_profile"
        android:title="@string/navigation_profile" />
    <item
        android:id="@+id/navigation_contactUs"
        android:icon="@drawable/navigation_contactUs"
        android:title="@string/navigation_contactUs" />
    <item
        android:id="@+id/navigation_logout"
        android:icon="@drawable/navigation_logout"
        android:title="@string/navigation_logout" />
</menu>
Lam Fung

Add this method to clear your menu item before inflating new menu.

mDrawer.Menu.Clear ();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to select item navigation drawer from fragment

From Java

How to close navigation drawer when an item is pressed from it?

From Dev

How to open Activity from Android's built-in navigation drawer

From Dev

How to pass data to navigation drawer item?

From Dev

Navigation drawer: How to know the current selected item?

From Dev

How to hide a navigation drawer menu item programmatically?

From Dev

How to add action to a Navigation Drawer item?

From Dev

How to add a link to a navigation drawer item?

From Dev

How to add Spinner as an item in Navigation Drawer

From Dev

Navigation Drawer: How to change the colour of item category?

From Dev

Add a item to a navigation drawer

From Dev

Get selected menu item's index value in the navigation drawer

From Dev

After change navigation drawer item's icon, app is dead

From Dev

Selecting item of sliding tabs menu from navigation drawer with some fragment

From Dev

How to open a new fragment from the navigation drawer?

From Dev

how to clear edittext from navigation drawer

From Dev

How to call main activity from the navigation drawer

From Dev

Add items in Navigation Drawer Dynamically

From Dev

How do I close a navigation drawer after an item is selected?

From Java

Navigation drawer: How do I set the selected item at startup?

From Dev

How to change selected Item in the navigation drawer depending on the activity/view?

From Dev

How to select the first item in a navigation drawer and open a fragment on application start

From Dev

Android: How to change the selected item in Navigation Drawer onBackPressed()?

From Dev

How to start an activity on material navigation drawer menu item clicked

From Dev

Android - How to click on an item on a navigation drawer using Espresso?

From Dev

How to add a collapsible menu item inside navigation drawer in android?

From Dev

How to add some space under the last item in a Navigation Drawer?

From Dev

How to draw a line between menu item in navigation drawer

From Dev

How do I close a navigation drawer after an item is selected?

Related Related

  1. 1

    How to select item navigation drawer from fragment

  2. 2

    How to close navigation drawer when an item is pressed from it?

  3. 3

    How to open Activity from Android's built-in navigation drawer

  4. 4

    How to pass data to navigation drawer item?

  5. 5

    Navigation drawer: How to know the current selected item?

  6. 6

    How to hide a navigation drawer menu item programmatically?

  7. 7

    How to add action to a Navigation Drawer item?

  8. 8

    How to add a link to a navigation drawer item?

  9. 9

    How to add Spinner as an item in Navigation Drawer

  10. 10

    Navigation Drawer: How to change the colour of item category?

  11. 11

    Add a item to a navigation drawer

  12. 12

    Get selected menu item's index value in the navigation drawer

  13. 13

    After change navigation drawer item's icon, app is dead

  14. 14

    Selecting item of sliding tabs menu from navigation drawer with some fragment

  15. 15

    How to open a new fragment from the navigation drawer?

  16. 16

    how to clear edittext from navigation drawer

  17. 17

    How to call main activity from the navigation drawer

  18. 18

    Add items in Navigation Drawer Dynamically

  19. 19

    How do I close a navigation drawer after an item is selected?

  20. 20

    Navigation drawer: How do I set the selected item at startup?

  21. 21

    How to change selected Item in the navigation drawer depending on the activity/view?

  22. 22

    How to select the first item in a navigation drawer and open a fragment on application start

  23. 23

    Android: How to change the selected item in Navigation Drawer onBackPressed()?

  24. 24

    How to start an activity on material navigation drawer menu item clicked

  25. 25

    Android - How to click on an item on a navigation drawer using Espresso?

  26. 26

    How to add a collapsible menu item inside navigation drawer in android?

  27. 27

    How to add some space under the last item in a Navigation Drawer?

  28. 28

    How to draw a line between menu item in navigation drawer

  29. 29

    How do I close a navigation drawer after an item is selected?

HotTag

Archive