0

I've been trying to create a navigation drawer for an existing app. I've found a few tutorials for this, but most of them (including the official Android guide) appear to be for the v4 ActionBarDrawerToggle library, which has been deprecated. I'm trying to use the v7 library instead, but my ActionBarDrawerToggle doesn't seem to do what the documentation says it should do.

Edit: Modified my code as per the answer below. The hamburger icon now switches back and forth correctly, but when the user taps the hardware back button to go back to the main fragment of my app, the hamburger icon disappears entirely. Why does this happen?

private void addDrawerItems() {
    String[] itemArray = {"About", "Nearby", "Settings", "Feedback",};
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, itemArray);
    mDrawerList.setAdapter(mAdapter);
    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d("ContributionsActivity", "Item " + position + " selected");
        }
    });
}

private void setupDrawer() {
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(R.string.title_activity_contributions);
    setContentView(R.layout.activity_contributions);

    //Set up navigation drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mDrawerList = (ListView)findViewById(R.id.drawer_list);
    addDrawerItems();
    setupDrawer();
    ...
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // enabling drawer toggle by clicking on the app icon.
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    } else {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (mediaDetails.isVisible()) {
                    getSupportFragmentManager().popBackStack();
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}
misaochan
  • 890
  • 2
  • 8
  • 25
  • 1
    try this ref:http://stackoverflow.com/questions/26754940/appcompatv7-v21-navigation-drawer-not-showing-hamburger-icon?rq=1 – Damini Mehra Aug 17 '16 at 08:33

1 Answers1

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

You're missing sync state, add it and everything should be fine.

Klawikowski
  • 605
  • 3
  • 11
  • Oh, wow, thanks! Will accept answer when the min time allows me to :) – misaochan Aug 17 '16 at 08:34
  • Oh, err, another issue now - when the user taps the hardware back button at any point, and goes back to the main fragment, the hamburger icon disappears. Why does this happen? – misaochan Aug 17 '16 at 08:41
  • 1
    It might be specific case according to your activity / fragment hierarhy. Try solutions in this link : http://stackoverflow.com/questions/17258020/switching-between-android-navigation-drawer-image-and-up-caret-when-using-fragme?rq=1 mDrawerToggle.setDrawerIndicatorEnabled(false); ? – Klawikowski Aug 17 '16 at 08:58
  • Ah, that link is very useful and solves another issue of mine, but I hadn't even called mDrawerToggle.setDra‌​werIndicatorEnabled(false) in the other fragments so unfortunately it doesn't solve this problem. – misaochan Aug 17 '16 at 09:11