Ad

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

Answer

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"/>
Ad
source: stackoverflow.com
Ad