Home » Android » android – Add item at bottom of navigation drawer

android – Add item at bottom of navigation drawer

Posted by: admin April 23, 2020 Leave a comment

Questions:

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!

How to&Answers:

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>