How to change hamburger icon in Android (NavigationDrawer)
Asked Answered
M

4

14

Before write this thread I have try to implement the different solution that I found in stackoverflow, but nothing work properly.

I'm developing an Android applucation that use the custom navigation drawer, I have to change the standard icon of actionbar (now is toolbar, right ?), and settings icon.

This is my code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitleTextColor(Color.parseColor("#009754"));
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

And this is what i try to implement:

This solution not work:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitleTextColor(Color.parseColor("#009754"));
toolbar.setNavigationIcon(R.drawable.ic_draw);
setSupportActionBar(toolbar);

This solution not work:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(false);
toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);

I don't understand why i can't change the icon, i have no idea what's the problem...

Meredithmeredithe answered 13/9, 2016 at 15:9 Comment(0)
F
1

If you're using the NavigationView with a DrawerLayout, you can set the hamburger icon programmatically like this;

 private fun setupToolbarWithCustomDrawerIcon() {
    val toggle = ActionBarDrawerToggle(
        this,
        binding.drawerLayout,
        binding.toolbar,
        R.string.navigation_drawer_open,
        R.string.navigation_drawer_close
    )

    binding.drawerLayout.addDrawerListener(toggle)
    toggle.syncState()
    binding.toolbar.setNavigationIcon(R.drawable.ic_drawer_menu)
}

Output:

enter image description here

Fabulous answered 23/2 at 11:46 Comment(0)
I
22

Simple and Elegant solution

Place

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.dashboardicon);//your icon here

after

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

Overall Solution in Navigation Activity

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.drawable.dashboardicon);

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

Note: It doesnt need any setNavigationOnClickListener()

Indefeasible answered 29/12, 2017 at 6:14 Comment(0)
D
13

Disable drawer indicator for ActionBarDrawerToggle:

toggle.setDrawerIndicatorEnabled(false);

and then:

toolbar.setNavigationIcon(R.drawable. ic_custom_drawer_icon);
Dumbbell answered 13/9, 2016 at 15:40 Comment(2)
Then add custom listener by calling setNavigationOnClickListener() on Toolbar as mentioned here.Scarab
toggle.setDrawerIndicatorEnabled(false); stops the icon from opening the navigation drawer. toolbar.setNavigationIcon(R.drawable. ic_custom_drawer_icon); should be enoughParrett
T
3
Just use this :

toolbar.post(new Runnable() {
            @Override
            public void run() {
                Drawable d = ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_launcher, null);
                toolbar.setNavigationIcon(d);
            }
        });
Timpani answered 7/12, 2020 at 5:58 Comment(0)
F
1

If you're using the NavigationView with a DrawerLayout, you can set the hamburger icon programmatically like this;

 private fun setupToolbarWithCustomDrawerIcon() {
    val toggle = ActionBarDrawerToggle(
        this,
        binding.drawerLayout,
        binding.toolbar,
        R.string.navigation_drawer_open,
        R.string.navigation_drawer_close
    )

    binding.drawerLayout.addDrawerListener(toggle)
    toggle.syncState()
    binding.toolbar.setNavigationIcon(R.drawable.ic_drawer_menu)
}

Output:

enter image description here

Fabulous answered 23/2 at 11:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.