Click here to Skip to main content
15,896,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a navigation drawer with full width on screen and below the toolbar. It opens from right to left. I am not using fragments.

In the activity calling it I need to have only the button toggle on the aligned ate end that opens it. It is OK.

My problem is: when it is opened, I need to place a Back Arrow (customized icon) at most left of toolbar and make it close the ND.

Activity Oncreate:
Java
this.toolbar = this.setCustomActionBar();

        this.chevron_back_icon = new IconicsDrawable(this)
                .icon(Icon.gmd_chevron_left)
                .color(Color.WHITE)
                .sizeDp(24);

        this.createDrawerMenu();

private Toolbar setCustomActionBar() {
    Toolbar toolbar = (Toolbar) this.findViewById(id.toolbar);
    this.setSupportActionBar(toolbar);
    this.supportActionBar = this.getSupportActionBar();
    if (this.supportActionBar != null) {
        this.supportActionBar.setDisplayHomeAsUpEnabled(false);
        this.supportActionBar.setDisplayShowHomeEnabled(false);
        this.supportActionBar.setDisplayShowTitleEnabled(false);
    }
    return toolbar;
}

private void createDrawerMenu() {
    this.drawer = (DrawerLayout) this.findViewById(id.drawer);
    ListView drawerList = (ListView) this.findViewById(id.list_view);

List<DrawerMenuItem> listItems = new ArrayList<>();
listItems.add(new DrawerMenuItem(Activity.ID_1, this.getString(string.menu_text_profile), drawable.ic_1));
listItems.add(new DrawerMenuItem(Activity.ID_2, this.getString(string.menu_text_payments), drawable.2));
listItems.add(new DrawerMenuItem(Activity.ID_3, this.getString(string.menu_text_my_wallet), drawable.3));
listItems.add(new DrawerMenuItem(Activity.ID_4, this.getString(string.menu_text_contact), drawable.ic_4));
listItems.add(new DrawerMenuItem(Activity.ID_5, this.getString(string.menu_text_logout), drawable.ic_5));

drawerList.setAdapter(new DrawerMenuAdapter(this, listItems));
drawerList.setOnItemClickListener(new DrawerItemClickListener());

this.toggle = new ActionBarDrawerToggle(
        this, this.drawer, this.toolbar, string.drawer_open, string.drawer_close);

this.toggle.setDrawerIndicatorEnabled(false);

this.drawer.addDrawerListener(this.toggle);
this.toggle.syncState();
this.toggle.setDrawerIndicatorEnabled(false);

NavigationView navigationView = (NavigationView) this.findViewById(id.nav_view);

int width = this.getResources().getDisplayMetrics().widthPixels;
LayoutParams params = (LayoutParams) navigationView.getLayoutParams();
params.width = width;
navigationView.setLayoutParams(params);
}


Toolbar.xml

Java
<?xml version="1.0" encoding="utf-8"?>
<!-- App bar -->
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="0dp"
    android:layout_marginStart="0dp"
    android:layout_marginTop="0dp"
    android:background="@color/primary_bar"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ToolBarStyle">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:gravity="center_vertical|center_horizontal"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/text_logo1
            android:textAlignment="center"
            android:textAllCaps="true"
            android:textColor="@color/yellow"
            android:textSize="36sp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" "
            android:textAlignment="center"
            android:textAllCaps="true"
            android:textSize="36sp"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/text_logo2"
            android:textAlignment="center"
            android:textAllCaps="true"
            android:textColor="@color/white"
            android:textSize="36sp"/>
    </LinearLayout>

</android.support.v7.widget.Toolbar>


activity.xml

XML
<include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>


        <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                android:id="@+id/drawer"
                                                android:layout_width="match_parent"
                                                android:layout_height="match_parent"

                                                tools:openDrawer="end">


            <RelativeLayout
                android:id="@+id/content"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <RelativeLayout
                    android:id="@+id/content2"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_centerVertical="false"
                    android:layout_marginBottom="?attr/actionBarSize">

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true">

            <android.support.design.widget.NavigationView
                android:id="@+id/nav_view"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="end"
                android:background="@color/drawer_background_black"
                android:fitsSystemWindows="true">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <include
                        android:id="@+id/drawer_header"
                        layout="@layout/nav_header_main"/>

                    <ListView
                        android:id="@+id/list_view"
                        android:layout_width="@dimen/drawer_option_width"
                        android:layout_height="match_parent"
                        android:layout_below="@id/drawer_header"
                        android:layout_gravity="start"
                        android:background="@color/drawer_background_grey"
                        android:choiceMode="singleChoice"
                        android:divider="@android:color/transparent"
                        android:dividerHeight="0dp"/>


                    </RelativeLayout>

                </RelativeLayout>

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

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

</RelativeLayout>


What I have tried:

Inflate a menu on Enf and use home buton.

Put call to close on home button.

Hidden home button.

Change the toolbar programaticlly.
Posted
Updated 18-Dec-16 5:41am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900