0

I'm trying to implement an ActionBarDrawerToggle into my app but I can't make it. I've achieved to show the toggle in my toolbar, but the icon is the same always.

This is toggle's icon when drawer is closed:

https://i.stack.imgur.com/HTcom.png

And this is when drawer is opened:

https://i.stack.imgur.com/dX0Z2.png

As you can see, it doesn't change from ic_drawer to back arrow. ic_drawer is never shown.

So, here's my activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    initializeDrawer();
    populateDrawer();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
}

private void initializeDrawer() {
        tagTitles = getResources().getStringArray(R.array.item_names);
        icons = getResources().getStringArray(R.array.item_icons);
        dwLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        dwList = (ListView) findViewById(R.id.drawer_list);
        dwList.setOnItemClickListener(new DrawerItemClickListener());
        dwToggle = new ActionBarDrawerToggle(this, dwLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle("pepe");
                invalidateOptionsMenu();
            }

            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle("pop");
                invalidateOptionsMenu();
            }
        };
        dwLayout.setDrawerListener(dwToggle);
    }

    private void populateDrawer() {
        ArrayList<DrawerItem> dwItems = new ArrayList<>();
        for (int i = 0; i < tagTitles.length; i++) {
//ignore this; population is not properly working
            dwItems.add(new DrawerItem(tagTitles[i], Resources.getSystem().getIdentifier("abc_ic_menu_copy_mtrl_am_alpha.png", "drawable", getBaseContext().getPackageResourcePath())));
        }

        dwList.setAdapter(new NavigationDrawerAdapter(this, dwItems));
    }

@Override
public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onPostCreate(savedInstanceState, persistentState);
    dwToggle.syncState();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (dwToggle.onOptionsItemSelected(item)) {
        return true;
    }

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    dwToggle.onConfigurationChanged(newConfig);
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }
}

Thanks!

alexhzr
  • 153
  • 1
  • 1
  • 12

1 Answers1

4

implement following method with DrawerLayout instace, in your case instance is dwLayout. implement following after this line

dwLayout.setDrawerListener(dwToggle);

add this

drawerLayout.post(new Runnable() {
    @Override
    public void run() {
        // To display hamburger icon in toolbar
        drawerToggle.syncState();
    }
});

or you can do this as well

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
Ajay P. Prajapati
  • 1,973
  • 1
  • 17
  • 34
  • Whoops! I forgot to add the method to the main post - It was already on my code. – alexhzr May 27 '15 at 18:26
  • Yes... it did work! But it is proper to start a thread just to change the dwToggle icon? Why is needed the thread and `mDrawerToggle.syncState()` on `onPostCreate()` doesn't work? – alexhzr May 27 '15 at 18:40
  • u can plus one if it worked. haha.. happy that it worked. . n since you're implementing your ActionbarDrawerToggle there u need to use runnable in that.. – Ajay P. Prajapati May 27 '15 at 18:56
  • Could you please provide a deeper explanation, please? It's just curiosity. – alexhzr May 27 '15 at 19:07
  • whenever you open or close the drawer, the activity needs to get notified in some way. syncState() method keeps it synchronize and sends that information when the drawer is opened or closed ..and sets the indicator. it is known as hamburger icon / indicator. hope you get my point. couldn't reply fast because i was busy, and please if you can,.. do plus indicating that your problem solved so other people can read and get answer. thanks! – Ajay P. Prajapati May 28 '15 at 00:05
  • Thanks for the `drawerLayout.post(...drawerToggle.syncState();)`. Without `post` it sometimes hadn't updated a hamburger state. – CoolMind Feb 08 '17 at 20:01