Changing Navigation drawer icon on action bar android
Asked Answered
M

10

39

I have created a NavigationDrawer in my app using the ActionBar. enter image description here

As showed in the picture above I want to change the NavigationDrawer toggle button icon to something I want. How can I change it?

Here is my code:-

mDrawerList.setOnItemClickListener(new SlideMenuClickListener());


    // enabling action bar app icon and behaving it as toggle button
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.hamburger_button, //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("Settings");
            // calling onPrepareOptionsMenu() to hide action bar icons
            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
@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);
    // Pass any configuration change to the drawer toggls
    mDrawerToggle.onConfigurationChanged(newConfig);
}

If I try changing it to R.drawable.hamburger_button It still shows the default icon

Metagnathous answered 30/6, 2014 at 4:10 Comment(5)
R.drawable.hamburger_button is your own custom image and not the same as the one provided by android ?Corrupt
yes. It is my custom image.Metagnathous
#9252854 please take a loot at this linkMalfunction
@HakHak this works. Thanks a lot. Please post a answer so that I accept it. Also any idea how I can I place the app title in the center of the action bar?Metagnathous
@Metagnathous no need mate.. because im doing that thing as well and was able to make it work i was just hoping it can help you as well happy coding :)Malfunction
S
61

To replace the drawer indicator icon with your own drawable(non animated) using the v7 ActionBarDrawerToggle, you can do the following:

//After instantiating your ActionBarDrawerToggle
mDrawerToggle.setDrawerIndicatorEnabled(false);
Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.your_custom_icon, getActivity().getTheme());
mDrawerToggle.setHomeAsUpIndicator(drawable);
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);
        }
    }
});
Swinge answered 20/8, 2015 at 11:46 Comment(4)
what should be the size of custom icon?Dianoia
perfect answer which includes the navigation listener which is important, otherwise the icon click is handled as going back.Courtnay
Not Perfect. Why doesn't this support dynamic size?Licentiate
This fixed my issueDetrimental
A
10

Try changing the icon manually by using setHomeAsUpIndicator() .

Like,

ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);

and

ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(...){};
mDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_drawer_toggle);
Alikee answered 9/8, 2015 at 18:43 Comment(0)
C
4

Please make sure you include these to sync the states of the icon properly.

@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);
}
Corrupt answered 30/6, 2014 at 4:21 Comment(0)
C
3

Under the initialization of ActionBarDrawerToggle write the following code:

toolbar.setNavigationIcon(R.drawable.ic_menu_camera);
Crusado answered 4/10, 2016 at 1:42 Comment(1)
The best answer, for my opinionDispenser
M
2

Here is a working solution:

    setSupportActionBar(toolbar2);
    toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar2, R.string.navigation_drawer_open,R.string.navigation_drawer_close);

    bottomNavigationView.setOnNavigationItemSelectedListener(this);
    navigationView=findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    toggle.syncState();

    //------------To change Navigation drawer icon ---------------//
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_favorite_black_24dp);

this Youtube video helped https://www.youtube.com/watch?v=biUaIO-N7Ew

Missal answered 11/6, 2020 at 4:2 Comment(0)
F
1
 mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.hamburger_button(This you the icon), //nav menu toggle icon
            R.string.app_name, // nav drawer open - description for accessibility
            R.string.app_name // nav drawer close - description for accessibility
            )
Fuller answered 9/1, 2015 at 13:47 Comment(2)
this is deprecated in favor of the v7 one and the v7ActionBarToggle doesn't have a constructor to change the icon.Intervale
To change the icon using v7 ActionBarDrawerToggle: https://mcmap.net/q/400497/-changing-navigation-drawer-icon-on-action-bar-androidSwinge
B
1

call super class methods of ActionBarDrawerToggle super.onDrawerClosed(view) and super.onDrawerOpened(drawerView) like

mDrawerToggle = new ActionBarDrawerToggle(...){
        public void onDrawerClosed(View view) 
         {
           super.onDrawerClosed(view);
           //---your code
         }

        public void onDrawerOpened(View drawerView) 
         {
           super.onDrawerOpened(drawerView);
           //---your code
         }
 }
Blaubok answered 25/2, 2015 at 13:7 Comment(0)
A
1

For me adding the setHomeAsUpIndicator worked.

mDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            mDrawerLayout,         /* DrawerLayout object */
            R.drawable.custom_icon,  /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description for accessibility */
            R.string.drawer_close  /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.custom_icon);
Argilliferous answered 18/7, 2016 at 11:46 Comment(0)
C
0

First of all you should in manifest try this code :

android:icon="@drawable/ic_icon1" 

This is image for total logo of your app

android:logo="@drawable/ic_drower" 

This is image for action bar

After that in main-activity try this code :

actionBar.setDisplayUseLogoEnabled(true);
Contumelious answered 11/4, 2016 at 7:32 Comment(0)
C
0
setSupportActionBar(yourToolbar);
yourToolbar.setNavigationIcon({yourDrawable});

Don't set Navigation Icon before setSupportActionBar()

Corrode answered 1/8, 2021 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.