9

I'm new with Android and in my app, I am using DrawerLayout, it's ok and fine. But I want to change ActionBarDrawerToggle icon.

How can we change it?

I tried a lot but I could not able to achieve the desired result, please help me someone?

xml:-

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:local="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            local:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </LinearLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

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

activity:-

public class MainActivity extends AppCompatActivity {
    ActionBarDrawerToggle actionBarDrawerToggle;
    DrawerLayout drawerLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
        drawerLayout.setDrawerListener(actionBarDrawerToggle);
    }
RoCkDevstack
  • 3,517
  • 7
  • 34
  • 56
Krish
  • 4,166
  • 11
  • 58
  • 110
  • Possible duplicate of [Change Icon Of Navigation Drawer](http://stackoverflow.com/questions/29558303/change-icon-of-navigation-drawer) – kandroidj Mar 21 '16 at 15:32

4 Answers4

15

add this line to your tool bar

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
ActionBarDrawerToggle mToogle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
    drawerLayout.addDrawerListener(mToogle);
    mToogle.syncState();
toolbar.setNavigationIcon(R.drawable.ic_your_image);

mToogle.syncState() will take care of actions(opening and closing the navigation drawer).

Irfan
  • 956
  • 9
  • 16
1

you must 3 steps:

  1. add actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
  2. add toolbar.setNavigationIcon(R.drawable.menu_icon);
  3. add OnClick to new Icon like below:

    toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { drawerLayout.openDrawer(Gravity.LEFT); } });

Haniyeh Khaksar
  • 774
  • 4
  • 20
0

After struggling with this for a few hours here's how I finally got it to work:

My implementation is using a Toolbar and android.support.v7.app.ActionBarDrawerToggle.

Use these lines in onCreate() to set the Toolbar as the support ActionBar

@Override
public void onCreate(Bundle savedInstanceState)
{
      toolbar = (Toolbar) findViewById(getToolbarId());

      setSupportActionBar(toolbar);
      ActionBar ab = getSupportActionBar();
      if (ab != null)
      {
            ab.setDisplayHomeAsUpEnabled(true);
            ab.setHomeButtonEnabled(true);
      }
}

Then you need to change the NavDrawer icon, you will do it with this code getSupportActionBar().setHomeAsUpIndicator(R.drawable.my_icon);. But you can't just call this from anywhere, you need to call it after syncing the DrawerToggle state.

So call it like so:

    drawerToggle.syncState();
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.my_icon);
Sakiboy
  • 7,252
  • 7
  • 52
  • 69
-1

It may be the correct one :-P

public class MainActivity extends AppCompatActivity {
    ActionBarDrawerToggle actionBarDrawerToggle;
    DrawerLayout drawerLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.navigation_drawer);
        drawerLayout = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                drawerLayout ,         /* DrawerLayout object */
                R.drawable.drawer_icon,  /* nav drawer icon to replace 'Up' caret */
                "drawer_open",  /* "open drawer" description */
                "drawer_close"  /* "close drawer" description */
                )
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.app_name,R.string.app_name);
        drawerLayout.setDrawerListener(actionBarDrawerToggle);
    }
Sathish Kumar
  • 919
  • 1
  • 13
  • 27
  • this line showing errors drawerLayout = new ActionBarDrawerToggle( this, /* host Activity */ mDrawerLayout, /* DrawerLayout object */ R.drawable.drawer_icon, /* nav drawer icon to replace 'Up' caret */ "drawer_open", /* "open drawer" description */ "drawer_close" /* "close drawer" description */ ) – Krish Mar 21 '16 at 15:21