Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Navigation drawer icon on action bar android

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

like image 945
user3713706 Avatar asked Jun 30 '14 04:06

user3713706


2 Answers

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);         }     } }); 
like image 199
Mateus Gondim Avatar answered Oct 14 '22 04:10

Mateus Gondim


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); 
like image 26
Msp Avatar answered Oct 14 '22 04:10

Msp