Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add footer buttons in NavigationView

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?

like image 608
Sandro Simas Avatar asked Oct 21 '15 23:10

Sandro Simas


People also ask

How do I add a footer layout in navigation drawer?

The simplest answer is to add a button inside the Drawer layout and set it gravity to bottom in the navigationview. xml . For few menu items, this might work. But as soon as you have many menu items, the button will overlap with the menu items on small devices or in landscape mode.

What is DrawerLayout?

DrawerLayout acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from one or both vertical edges of the window.

What is NavigationView in Android?

com.google.android.material.navigation.NavigationView. Represents a standard navigation menu for application. The menu contents can be populated by a menu resource file. NavigationView is typically placed inside a DrawerLayout .


1 Answers

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

like image 88
Mohammed Fadhl Avatar answered Nov 02 '22 05:11

Mohammed Fadhl