Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change hamburger icon in Android (NavigationDrawer)

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

like image 454
Mattia Avatar asked Sep 13 '16 15:09

Mattia


People also ask

How do I show the hamburger icon on my Android?

Here's an example of the hamburger icon being used in the Gmail app for Android. Tapping the icon opens a menu from that side of the screen. The hamburger icon can also be found in the Windows 10 Start Menu. Hovering over or clicking the icon expands the shortcuts for Settings, Power, and other user options.

How do I change the icon size on my navigation drawer Android?

You can change the size of Navigation Drawer icons by overriding design_navigation_icon_size attribute in dimens. xml. This is a better answer than the first.


3 Answers

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

like image 150
Rajesh Naddy Avatar answered Oct 08 '22 14:10

Rajesh Naddy


Disable drawer indicator for ActionBarDrawerToggle:

toggle.setDrawerIndicatorEnabled(false);

and then:

toolbar.setNavigationIcon(R.drawable. ic_custom_drawer_icon);
like image 10
klimat Avatar answered Oct 08 '22 15:10

klimat


Just use this :

toolbar.post(new Runnable() {
            @Override
            public void run() {
                Drawable d = ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_launcher, null);
                toolbar.setNavigationIcon(d);
            }
        });
like image 2
Sakibul Haque Avatar answered Oct 08 '22 16:10

Sakibul Haque