Why I am getting wrong icon when using navigation drawer?

961 views Asked by At

I am building application with navigation drawer. I want to add ic_drawer icon to it. I am using this for it:

mDrawerToggle = new ActionBarDrawerToggle(
        this, /* host Activity */
        mDrawerLayout, /* DrawerLayout object */
        R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
        R.string.drawer_open, /* "open drawer" description */
        R.string.drawer_close /* "close drawer" description */
) { ... }

    // Set the drawer toggle as the DrawerListener
    mDrawerLayout.setDrawerListener(mDrawerToggle);

Actually, I got wrong icon and also I don't have animation on it. How I can fix it? I can't rly find anything, except drawable.ic_drawer in my code.

Screens are below.

Blank screen screenshot. enter image description here

Opened menu screen.

enter image description here

You can see on both images, that it is not ic_drawer image. Actually, in IDEA, I see correct image.

4

There are 4 answers

1
Vikalp Patel On

Have you add DrawerLayout listener with your ActionBarDrawerToggle? If not please add up these :

mDrawerLayout.setDrawerListener(mDrawerToggle);//mDrawerLayout is DrawerLayout
2
Moinkhan On

Make sure You are using v7 ActionBarDrawerToggle

mActionBarDrawerToggle = new ActionBarDrawerToggle(
        this, your_drawer_layout, your_tool_bar, R.string.open, R.string.close
);

As you can see v7 ActionBarDraweToggle have different constructor and doesn't ask for your Icon. because it takes automatically from android resources.

1
Hogun On

You can refer Creating a Navigation Drawer in Android Developer.

Few peoples skip this codes below. but the code is very important to show correct icon.
I hope to you read more reference.

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

if you are using latest SDK, you may use compat-v7 and Toolbar.
I experienced burger icon isn't show. so I need other way.
finally solved using compt-v7 and Toolbar.

0
voghDev On

@Moinkhan is right. You need to replace your constructor from v4 to the new one from v7. Replace this old code:

mDrawerToggle = new ActionBarDrawerToggle(
    getActivity(),                    
    mDrawerLayout,                    
    R.drawable.ic_drawer,
    "Open drawer",  
    "Close drawer"
)

for this one

import android.support.v7.app.ActionBarDrawerToggle;

(remove old import from .v4.app.ActionBarDrawerToggle)

mDrawerToggle = new ActionBarDrawerToggle(
    getActivity(), 
    mDrawerLayout, 
    "Open drawer", 
    "Close drawer"
);

It should now show an animated drawer icon that turns into left arrow when it is expanded