1

I've been trying to make my Navigation Drawer open using the ActionBarActivity's Up button for a few hours now, but I just can't seem to work it out.

Right now, I can open it by swiping/sliding right and I can see the arrow Up/Back button in the ActionBar, but the Navigation Drawer won't open once I tap the button.

Please note I'm using Support v7 ActionBarDrawerToggle.

Here's my ActionBarActivity's onCreate:

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new HomeFragment())
                .commit();
    }

    Log.d(TAG, "onCreate");

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerToggle.setDrawerIndicatorEnabled(true);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    Log.d(TAG, "onPostCreate");
    mDrawerToggle.syncState();
}

Am I missing something? Perhaps there's a method call that links the ActionBar's Up/Back button to the DrawerToggle?

Any help/guidance is very well appreciated.

Update: I also tried using mDrawerToggle.syncState(); and nothing changed. Updated the onCreate method above to include the syncState call.

Update 2: I updated the code again to how it currently stands in my MainActivity file. I made a few changes as suggested but the drawer still won't open.

I've tested this in two devices: in an HTC One m7 with Android 5.0.2 and Sense 6.5 and in an x86 AVD Emulator running Lollipop SDK 21.

Cramps
  • 464
  • 6
  • 16

4 Answers4

4

Take a look at my codes first:

public class HomeActivity extends ActionBarActivity implements
    DrawerCloseListener {
private Toolbar toolbar;
private DrawerLayout drawer;
private ActionBarDrawerToggle drawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    toolbar = (Toolbar) findViewById(R.id.home_toolbar);
    toolbar.setNavigationIcon(R.drawable.icon_nav);
    setSupportActionBar(toolbar);
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerToggle = new ActionBarDrawerToggle(this, drawer, toolbar,
            R.string.app_name, R.string.app_name);
    drawerToggle.setHomeAsUpIndicator(R.drawable.icon_nav);
    drawer.setDrawerListener(drawerToggle);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    return super.onOptionsItemSelected(item);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    if (drawer.isDrawerOpen(Gravity.LEFT | Gravity.START)) {
        drawer.closeDrawers();
        return;
    }
    super.onBackPressed();
}

@Override
public void onDrawerClose() {
    // TODO Auto-generated method stub
    if (drawer.isDrawerOpen(Gravity.LEFT | Gravity.START)) {
        drawer.closeDrawers();
    }
}
}

And among codes above, I replaced ActionBar by ToolBar, but you still can use ActionBar where there is a ToolBar. Did you miss something?

SilentKnight
  • 13,761
  • 19
  • 49
  • 78
  • 1
    Can't this be done without having to replace the default ActionBar with a Toolbar? This might work but it seems too tedious for a seemingly simple task. I'll try this if I can't make this work without replacing the default ActionBar. Thank you anyway! I'll let you know if this works. – Cramps Apr 17 '15 at 05:51
  • `ToolBar` is a generalization of `ActionBar`. Where you can use `ActionBar`, you can use `ToolBar`. – SilentKnight Apr 17 '15 at 07:19
  • I just finished implementing it the "Toolbar way" and it works as intended. Thank you! :) – Cramps Apr 17 '15 at 07:23
  • I would suggest removing some of the unnecessary code (such as setting ActionBar/Toolbar titles and whatnot). I did suggest an edit leaning it down a bit, leaving it with the minimum required code for the drawer to work but it seems to have been either lost or rejected. Kudos. – Cramps Apr 18 '15 at 01:40
0

You need to sync your drawer toggle in order to get the up button to well ... sync :)

mDrawerToggle.syncState();

Charles Durham
  • 2,445
  • 16
  • 17
  • I tried this shortly before asking this question and it didn't work, either. My apologies! I missed adding it to the `onCreate` code above since I undid that change after trying it. I'll update my question. – Cramps Apr 17 '15 at 02:40
  • Have you tried moving `.syncState()` into `onPostExecute()` http://stackoverflow.com/a/20655364/3474528 – Charles Durham Apr 17 '15 at 02:49
  • I suppose you meant `onPostCreate`? I just tried and nothing changed. I don't understand what's wrong here. – Cramps Apr 17 '15 at 03:11
0

I think you should add

mDrawerToggle.setDrawerIndicatorEnabled(true);

and move this line mDrawerLayout.setDrawerListener(mDrawerToggle); after mDrawerToggle = new ActionBarDrawerToggle(...);

Edit: After I checked my code again, the closing and opening is handled in separate method

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        if(mDrawerLayout.isDrawerOpen(drawerList)) {
            mDrawerLayout.closeDrawer(drawerList);
        }
        else {
            mDrawerLayout.openDrawer(drawerList);
        }
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}
inmyth
  • 8,880
  • 4
  • 47
  • 52
  • I moved the lines and added the `setDrawerIndicatorEnabled(true)` right after but nothing's different. I'll update the question with my current code. – Cramps Apr 17 '15 at 05:38
  • That's a good idea, however when I remove those lines the drawer icon in the ActionBar disappears altogether! How did you add the DrawerToggle's icon to the ActionBar? – Cramps Apr 17 '15 at 05:48
  • You mean the left arrow icon ? By calling mDrawerToggle.setDrawerIndicatorEnabled(true); If you use white or black background, this icon might be buried because it's also white or black (depending on themes). Or you can also also set it as false and use mDrawerToggle.setHomeAsUpIndicator(R.drawable.custom_icon); to use your own custom icon. – inmyth Apr 17 '15 at 05:57
  • I see. None of those made it appear. Perhaps the mDrawerToggle's context is wrong? Am I supposed to pass it the ActionBarActivity in the constructor? – Cramps Apr 17 '15 at 06:05
  • 1
    What would you say to replacing ActionBar to Toolbar ? My code is working on Toolbar which is generalized ActionBar which you can place anywhere on the layout not just on top. – inmyth Apr 17 '15 at 06:08
0

You need to change order of two lines :

1.

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close);

then 2.

 mDrawerLayout.setDrawerListener(mDrawerToggle);

Because while setting setDrawerListener the object mDrawerToggle was not initialized

Hope this will solve your work

Kushal
  • 8,100
  • 9
  • 63
  • 82