Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add buttons to navigation drawer menu?

Tags:

I want to add a button to my navigation drawer menu, like so:

desired result:

Image: desired result

I tried achieving this using the actionLayout parameter, but I seem to only be able to use some space on the right, not the entire width:

current result:

Image: current result

The title seems to be occupying the space on the left. But I want to add a button with full width like in the first picture.

My current code:

...

<item
                    android:id="@+id/nav_login"
                    android:title=""
                    app:actionLayout="@layout/button_login"
                    app:showAsAction="ifRoom"/>

...

button_login.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="#0000ff"
    android:text="Login"
    android:textColor="#ffffff"
    android:layout_height="match_parent" />
like image 755
Lukas Schneider Avatar asked May 09 '19 11:05

Lukas Schneider


1 Answers

Action view in drawers intended to show this "small" addition view on the right of the menu item, so it'll be restricted in size.

You can add desired button as some sort of footer like following:

<android.support.design.widget.NavigationView
    android:id="@+id/drawer"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/drawer">
    <Button
       android:id="@+id/footer_item_1"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_gravity="bottom"
       android:text="Footer Button 1" />
</android.support.design.widget.NavigationView>

Note that you can put everything you want as a child, if you want some more views - just add LinearLayout there and put all additional views as children of it.

like image 119
Ekalips Avatar answered Oct 25 '22 17:10

Ekalips