48

I'm a newbie on android studio and I've been trying to learn how to use the navigation drawer layout. I'm using the standard template for Nav Drawer, and wanted to add an item at the bottom of the menu, like this:

nav drawer image

at first i thought that the items in the drawer were inside a LinearLayout and it would be fairly easy to do what i want, but the xml code looks like this:

<?xml version="1.0" encoding="utf-8"?>

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_camera"
        android:icon="@drawable/ic_menu_camera"
        android:title="OP1" />
    <item
        android:id="@+id/nav_gallery"
        android:icon="@drawable/ic_menu_gallery"
        android:title="OP2" />
    <item
        android:id="@+id/nav_slideshow"
        android:icon="@drawable/ic_menu_slideshow"
        android:title="OP3" />
    <item
        android:id="@+id/nav_manage"
        android:icon="@drawable/ic_menu_manage"
        android:title="OP4" />
</group>


<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_logout"
        android:title="Log out" />
</group>

is there a way to make the group containing the logout item to be at the bottom? Thanks in advance for your answers!

Avrgebro
  • 848
  • 1
  • 8
  • 16
  • 2
    Maybe look at this: https://stackoverflow.com/questions/30543605/how-to-add-footer-to-navigationview-android-support-design-library – CodexNZ Aug 10 '17 at 23:12

1 Answers1

123

Try this

Remove this part

<group android:checkableBehavior="single">
    <item
        android:id="@+id/nav_logout"
        android:title="Log out" />
</group>

then go to your layout where the navigation view is located.

<android.support.design.widget.NavigationView
    android:id="@+id/navigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_account_navigation"
    app:menu="@menu/menu_lender_nav">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:orientation="vertical"
        android:padding="16dp">

        <TextView
            android:id="@+id/logout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Log out" />
    </LinearLayout>

</android.support.design.widget.NavigationView>
Morgan Koh
  • 2,297
  • 24
  • 24
Sakura Fukuyoshi
  • 1,461
  • 1
  • 10
  • 14
  • 1
    This worked, thanks! but you don't need to wrap in the LinearLayout – Lafayette Nov 16 '17 at 16:18
  • 3
    In landscape, it looks like the menus OP4, OP5 gets hidden with this solution. – User12111111 Jan 10 '18 at 14:06
  • 5
    Nice but If the list is long then it overlap with the view that we added in the bottom! any solution for that?? – Jay Patel Jan 24 '18 at 08:57
  • 3
    Yes @Keselme, In my case if I take the smallest phone screen my list exceeds 4 items to be scrolled so I've added the dummy list item(with enabled = false property) at the end so it will occupy the space which is covered by the logout button at the end and a user can see all the items. – Jay Patel Mar 11 '18 at 17:17
  • In the case where you have multiple navigation items in that bottom list, how would you deal with them in code? It looks like you'd need to have event listeners for each extra item, and then have to coordinate the view state between items in the main list vs the ones on the bottom. Is there an easy way to leverage the fact that they're all children of a navigation menu and therefore treat them as menu items? – pingOfDoom Jul 25 '19 at 15:16