31

I am having trouble with changing my navigation drawer icon to a custom one. I've currently had to implement the standard drawer icon which has 3 horizontal lines on top, but now I want to replace this with my custom drawer icon.

This is how my mDrawerToggle is at the moment:

mDrawerToggle=new ActionBarDrawerToggle(this,
    mDrawerLayout,
    R.drawable.app_icon,
    R.string.drawer_open) {
        // My code
    };
Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
Tufan
  • 2,789
  • 4
  • 34
  • 52

5 Answers5

40

Use below code,it's working for V7 ActionBarDrawerToggle

mDrawerToggle.setDrawerIndicatorEnabled(false);

mDrawerToggle.setHomeAsUpIndicator(R.drawable.your_custom_icon);
 mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
 public void onClick(View v) {
     if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
          mDrawerLayout.closeDrawer(GravityCompat.START);
     } else {
         mDrawerLayout.openDrawer(GravityCompat.START);
    }
}
});
Nabeel K
  • 5,938
  • 11
  • 38
  • 68
Prashant Date
  • 433
  • 4
  • 14
  • 2
    Suggestion: You can use the Drawable resource id for setting an image file as a Navigation Drawer Icon. Example: mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_navigation_home_icon); Hence, no need of using the below code: Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.your_custom_icon, getActivity().getTheme()); – udai Oct 10 '16 at 08:42
  • This was the best option for me. Setting the icon explicitly, rather than letting Android figure out whether to show a menu icon or back icon, is a lot more consistent for my app. – dustinrwh Sep 29 '17 at 05:19
  • Why doesn't the icon auto-resize. When I use a 512*512 icon, It takes up the entire ActionBar. – grantespo Mar 02 '19 at 01:31
7

Here is the sample code taken from Creating a Navigation Drawer

Activity.class

public class MainActivity extends Activity {
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;
    ...

    public void onCreate(Bundle savedInstanceState) {
        ...

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                getActionBar().setTitle(mTitle);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                getActionBar().setTitle(mDrawerTitle);
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Pass the event to ActionBarDrawerToggle, if it returns
        // true, then it has handled the app icon touch event
        if (mDrawerToggle.onOptionsItemSelected(item)) {
          return true;
        }
        // Handle your other action bar items...

        return super.onOptionsItemSelected(item);
    }

    ...
}
Shvet
  • 1,159
  • 1
  • 18
  • 30
Tarun Tak
  • 421
  • 1
  • 5
  • 12
  • Yar @Tarun jain i had done same thing ...what is my problem check that... i know constructor of mdrawertoogle – Tufan Apr 10 '15 at 10:25
  • @Tufan Did u added the following code.. `@Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); // Sync the toggle state after onRestoreInstanceState has occurred. mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); }` – Tarun Tak Apr 10 '15 at 10:26
  • i had override both the method ....look at my pics my icon is displayed but when i wanna change ..it dosent change – Tufan Apr 10 '15 at 10:30
  • if i add R.String.drawer_close then it gives me error change construcitr or change ic_drawer to toolbar..any soln – Tufan Apr 10 '15 at 10:59
  • Can u check the import whether it is using V4 support library or not – Tarun Tak Apr 10 '15 at 11:41
  • ...yeh it is imported from b4 library – Tufan Apr 10 '15 at 12:05
  • Check if you have added dependencies in build.gradle file or not if you are using Android Studio. In Eclipse check the android support library. You should not add both V4 and V7 library. – Tarun Tak Apr 10 '15 at 12:14
  • i had done it already ..i had accepted ur ans ..now give me upvote @tarun Tak – Tufan Apr 10 '15 at 12:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74935/discussion-between-tufan-and-tarun-tak). – Tufan Apr 10 '15 at 12:47
  • This CTOR doesn't exist. Only similar thing is having DrawerArrowDrawable instead of the drawable resource – android developer Dec 06 '17 at 14:48
  • 1
    NOT WORKING NOW – Kishan Solanki Aug 18 '20 at 10:42
5

This is main activity

final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);


        toggle.setDrawerIndicatorEnabled(false);

        toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                drawer.openDrawer(GravityCompat.START);
            }
        });

        toggle.setHomeAsUpIndicator(R.drawable.menuicon);
codaman
  • 206
  • 4
  • 6
0

You could use this format for your mDrawerToggle:

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
    R.drawable.CUSTOM_ICON, // Navigation menu toggle icon
    R.string.DRAWER_OPEN, // Navigation drawer open description
    R.string.DRAWER_CLOSE // Navigation drawer close description
    )

Change your drawable and make sure it is the same name as the one in the code.

Farbod Salamat-Zadeh
  • 19,687
  • 20
  • 75
  • 125
0

This is the main layout file

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

    <!-- Framelayout to display Fragments -->

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- Listview to display slider menu -->

    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:choiceMode="singleChoice"
        android:divider="@color/black"
        android:dividerHeight="1dp" />

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

This is the main Activity

DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.menuicon, // nav menu toggle icon
                R.string.app_name, // nav drawer open - description for
                                    // accessibility
                R.string.app_name // nav drawer close - description for
                                    // accessibility
        ) {
            public void onDrawerClosed(View view) {
                // getActionBar().setTitle(mTitle);
                // calling onPrepareOptionsMenu() to show action bar icons
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                // getActionBar().setTitle(mDrawerTitle);
                // calling onPrepareOptionsMenu() to hide action bar icons
                invalidateOptionsMenu();
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

finally at R.drawable.menuicon(you can give your image id) it will work.

Hanuman
  • 958
  • 13
  • 35