2

I am using NavigationDrawer in android. its working properly. now i want to change image of NavigationDrawer backbutton so for that, i used this mDrawerToggle.setDrawerIndicatorEnabled(false); it hide the default backbutton image. and now change the image using this mDrawerToggle.setHomeAsUpIndicator(R.mipmap.ic_launcher); but on click of custom navigation back button drawer not working.How to manage this?

my code is

 public void setUp(int fragmentId, DrawerLayout drawerLayout, final Toolbar toolbar) {
    containerView = getActivity().findViewById(fragmentId);
    mDrawerLayout = drawerLayout;
    mDrawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
        @Override
        public void onDrawerOpened(View drawerView) {
            is= false;
            super.onDrawerOpened(drawerView);

            getActivity().invalidateOptionsMenu();
        }
        @Override
        public void onDrawerClosed(View drawerView) {
            is= true;
            super.onDrawerClosed(drawerView);

            getActivity().invalidateOptionsMenu();
        }
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {
            super.onDrawerSlide(drawerView, slideOffset);

        }
    };

    mDrawerToggle.setHomeAsUpIndicator(R.mipmap.ic_launcher);
mDrawerToggle.setDrawerIndicatorEnabled(false);

    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mDrawerToggle.syncState();
        }
    });


}
aj0822ArpitJoshi
  • 1,142
  • 1
  • 9
  • 25

2 Answers2

0

To change the icon of NavigationDrawer backbutton, you need to call following method, with your image resourceid:

mDrawerToggle.setHomeAsUpIndicator(resId);

and setDrawerIndicatorEnabled() method should not be set to false to update the icon:

mDrawerToggle.setDrawerIndicatorEnabled(false);

because is setDrawerIndicatorEnabled(), is set to false it will do two things:

  1. disables the HomeAsUpIndicator icon
  2. disables the clickListener.

Also you can try it with actionbar:

The below function is added in android API level 18 and upper.

ActionBar().setHomeAsUpIndicator(R.drawable.ic_yourindicator);

If you use support library, try this:

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

Also you can try it with Styles:

The "up" affordance indicator is provided by a drawable specified in the homeAsUpIndicator attribute of the theme. To override it with your own custom version it would be something like this:

<style name="Theme.MyFancyTheme" parent="android:Theme.Holo">
    <item name="android:homeAsUpIndicator">@drawable/my_fancy_up_indicator</item>
</style>

If you are supporting pre-3.0 with your application be sure you put this version of the custom theme in values-v11 or similar.

if it's still not working, you can try by replacing:

<item name="android:homeAsUpIndicator">@drawable/my_fancy_up_indicator</item>

with:

<item name="homeAsUpIndicator">@drawable/my_fancy_up_indicator</item>
Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61
0

Add this to your option menu selected listener.

@Override

public boolean onOptionsItemSelected(MenuItem item) {
    Intent intent;
    switch (item.getItemId()) {

        case android.R.id.home:
            onBackPressed();
            break;

    }
    return true;
}