11

I'm following the post "Android design library NavigationView with footer" to add buttons at the bottom of NavigationView. The problem is that only the last NavigationView appears and it's occuping the entire screen height.

Here is my layout:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Activity here -->

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start">

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_menu_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="top"
            app:menu="@menu/menu_drawer"/>

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_footer_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            app:menu="@menu/menu_drawer_footer"/>

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

Here is the result:

enter image description here

How can i fix this?

Sandro Simas
  • 1,268
  • 3
  • 21
  • 35
  • That blog is a few months old and the design support library has had a few updates in that time. In particular NavigationView now uses a RecyclerView internally instead of a ListView. It's possible that this trick just doesn't work anymore. Try it out using an older build of the library and see if that changes anything. – RussHWolf Oct 22 '15 at 01:56
  • @Brucelet, do you known another solution? – Sandro Simas Oct 22 '15 at 15:07
  • 1
    Confirmed, on 23.0.1 this trick works, on 23.1 doesn't work – Alex Sorokoletov Nov 16 '15 at 21:24
  • Any news on this? I also need a sticked footer with the support NavigationView! – chrisonline Feb 25 '16 at 18:04
  • I have answerd this with and easy example here http://stackoverflow.com/a/38476562/5817706 – Mohammed Fadhl Jul 20 '16 at 08:57

2 Answers2

17

The simplest answer is to add a button inside the Drawer layout and set it gravity to bottom in the navigationview.xml

as easy as that !!! here is the code.

<android.support.design.widget.NavigationView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/navigation"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/navigation_header"
    app:menu="@menu/menu_navigation"
    android:layout_alignParentTop="true">

   <Button
        android:id="@+id/btn_sing_in"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="@string/sign_in"
        android:layout_gravity="bottom"
       />
</android.support.design.widget.NavigationView>

add button inside Drawer layour

Mohammed Fadhl
  • 630
  • 12
  • 22
12

Weights and margins may need to be added or tweaked depending on how many items you want top and bottom, but this should be what you're looking for:

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_drawer_container"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="10">

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_drawer"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="top"
        android:layout_weight="8"
        app:menu="@menu/drawer" />

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_drawer_bottom"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:layout_gravity="bottom"
        app:menu="@menu/drawer_footer"  />
    </LinearLayout>
</android.support.design.widget.NavigationView>

Hope this helps :)

devchimp
  • 754
  • 6
  • 18
  • 6
    You need to set `app:elevation=0dp` to the inner NavigationViews else the footer will have a strange gradient background. Unless there's a way to remove the scrolling effect of the footer, I stick to using simple TextViews in the footer. However, it's a good solution. – headsvk Jan 14 '16 at 10:14