Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we change ActionBarDrawerToggle icon?

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);
    }
like image 607
Krish Avatar asked Mar 21 '16 15:03

Krish


People also ask

Where is my Navigation menu icon?

The Navigation icon(displayed as ☰) is actually a button in the action bar and is known as the hotdog or hamburger button.It is often placed on the top left or top right of a user interface and is so called for its resemblance to a hotdog or hamburger.


2 Answers

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).

like image 168
Irfan Avatar answered Oct 28 '22 08:10

Irfan


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); } });

like image 29
Haniyeh Khaksar Avatar answered Oct 28 '22 09:10

Haniyeh Khaksar