How to add DrawerLayout to my ToolBar / ActionBar

Stephanie

I have a custom Toolbar that I've set up in my MainActivity to be the top action bar. The question is, how do you implement a NavigationDrawer to display when the ImageView in the toolbar is clicked?

In MainActivity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_main_drawer);

    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

My layout_main_drawer

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context="com.XXXX" tools:ignore="MergeRootFrame" />

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:minHeight="?attr/actionBarSize"
    android:background="#ccc">
    <ImageView
        android:id="@+id/toolbar_action"
        android:src="@drawable/ic_drawer"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginRight="50dp"/>
</android.support.v7.widget.Toolbar>

Gabriele Mariotti

You can use something like this:

    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
        this,  mDrawerLayout, mToolbar,
        R.string.navigation_drawer_open, R.string.navigation_drawer_close
    );
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    mDrawerToggle.syncState();

Pay attention. android.support.v4.app.ActionBarDrawerToggle is deprecated.

You have to use the android.support.v7.app.ActionBarDrawerToggle. This class has a constructor with the Toolbar.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I use DrawerLayout to display over the ActionBar/Toolbar and under the status bar?

From Dev

Android ActionBar/Toolbar Disappears in my App

From Dev

Add custom layout to ActionBar/Toolbar with no margins

From Dev

ActionBar vs Toolbar or ActionBar and Toolbar

From Dev

How to hide my ActionBar

From Dev

How to hide my ActionBar

From Dev

Android: How to/Tutorial for changing ActionBar to ActionBarCompat (Toolbar)?

From Dev

How to make SearchView that covers the entire ActionBar/Toolbar

From Dev

How to create resizable ToolBar (or ActionBar) in android

From Dev

How to add a button to actionbar?

From Dev

How to add a button to actionbar?

From Dev

Collapsing Toolbar and DrawerLayout

From Dev

Collapsing Toolbar and DrawerLayout

From Dev

How do I make DrawerLayout to display below the Toolbar?

From Dev

How to make toolbar transparent programmatically while using a DrawerLayout and Fragments

From Dev

These two icons(drawerlayout and menu icon) in toolbar is black, How to turn it into white?

From Dev

How to add stylesheet to toolbar

From Dev

How to add Toolbar in PreferenceActivity

From Dev

how to add image to toolbar

From Dev

How to add an customized button on the actionbar?

From Dev

How to Hide ActionBar/Toolbar While Scrolling Down in Webview

From Dev

Toolbar and ActionBar not shown in activity

From Dev

Android ActionBar or Toolbar

From Dev

Error setting Toolbar as ActionBar

From Dev

Migrate from ActionBar to ToolBar

From Dev

Android Hide actionbar and Toolbar

From Dev

Delete toolbar actionBar

From Dev

Problems with AutoCompleteTextView on Actionbar/Toolbar

From Dev

Android toolbar + actionbar

Related Related

HotTag

Archive