Mamo1234
Mamo1234

Reputation: 998

How to add buttons to navigation drawer menu?

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" />

Upvotes: 6

Views: 10368

Answers (2)

Mamo1234
Mamo1234

Reputation: 998

My solution is now using the MaterialDrawer Library.

I just did a quick test and it solves the problem:

enter image description here

Code:

...
Drawer drawer = new DrawerBuilder()
                .withActivity(this)
                .withToolbar(findViewById(R.id.toolbar))
                .addDrawerItems(
                        new PrimaryDrawerItem().withName("Entry 1"),
                        new PrimaryDrawerItem().withName("Entry 2"),
                        new AbstractDrawerItem() {
                            @Override
                            public RecyclerView.ViewHolder getViewHolder(View v) {
                                return new RecyclerView.ViewHolder(v) {
                                };
                            }

                            @Override
                            public int getType() {
                                return 0;
                            }

                            @Override
                            public int getLayoutRes() {
                                return R.layout.button_a;
                            }

                            @Override
                            public Object withSubItems(List subItems) {
                                return null;
                            }

                            @Override
                            public Object withParent(IItem parent) {
                                return null;
                            }
                        },
                        new PrimaryDrawerItem().withName("Entry 3")
                )
                .build();
...

button_a.xml

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_width="match_parent"
        android:text="Login"
        android:layout_height="50dp"/>

Upvotes: 1

 Ekalips
Ekalips

Reputation: 1491

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.

Upvotes: 3

Related Questions