9

I'm trying to allow the user to open/close the navigation drawer in my app by tapping the action bar title (this is how the current Android Gmail app is set up). At the moment, the user can toggle the drawer by tapping the app/drawer icon or by sliding it in with a left-right swipe. However, the action bar title itself is not clickable. According to the developer docs, clicking the action bar title should "dispatch onOptionsItemSelected to the host Activity with a MenuItem with item ID android.R.id.home" when we use NAVIGATION_MODE_STANDARD but for some reason I can't get the title to behave this way.

I believe the Navigation Drawer itself is fine, but here is how I set up the Action Bar:

private void configureActionBar(CharSequence mTitle) {

    ActionBar actionBar = getActionBar();

    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);

    actionBar.setIcon(R.drawable.ic_blank);

    GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM,
                new int[] {
                0xFF004700, 0xFF002900
                });

    gd.setCornerRadius(0f);

    actionBar.setBackgroundDrawable(gd);

    // set the title of the action bar using the given title
    actionBar.setTitle(mTitle);

}

Any suggestions would be greatly appreciated!

David Crozier
  • 1,398
  • 2
  • 13
  • 14

2 Answers2

24

If you want the drawer to open by tapping Icon/Title of ActionBar, i suggest you to use the ActionBarDrawerToggle class provided in the support library(android.support.v4.app.ActionBarDrawerToggle)

Reference: https://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html

Example of use:
https://developer.android.com/training/implementing-navigation/nav-drawer.html

The trick comes when capturing the event in onOptionsItemSelected(), that you have to pass it to the ActionBarDrawerToggle, so it can handle the open/close drawer request:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
      return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
nsL
  • 3,722
  • 3
  • 23
  • 40
  • Thank you for the post - I actually already pass the event to mDrawerToggle as you outlined (which is why tapping the app icon toggles the drawer as expected), but I'm having trouble getting the same functionality from the ActionBar title (separate from the icon, as in the Gmail app). I suppose it might be possible to use a custom view for the ActionBar with a TextView as the title - then I could set up an onClick listener for the view and call openDrawer(), but this seems less elegant than the solution I'm hoping to find. Thank you though! – David Crozier Sep 06 '13 at 17:56
  • Mmm strange because the current app im doing it works also for the title.in fact icon+title acts like an only button because it highlights both whatever i click. Maybe there is a problem using ActionBarDrawerToggle which is from support library with the ActionBar which is not. Tried using the ActionBar from support library? Youll have to do getSupportActionBar() instead of getActionBar() ... Just guessing – nsL Sep 06 '13 at 18:43
  • Ah, good catch. I haven't been using the SupportActionBar, so I'll be sure to try it. Thanks! – David Crozier Sep 06 '13 at 19:23
  • @DSCroz please, accept answer as correct if it has helped you! thanks – nsL Dec 01 '13 at 10:24
0

The Icon/Title is shown if application theme attribute is set in AndroidManifest.xml like this:

    <application
    android:name=".SampleApp"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

res/values/styles.xml holds the theme declaration

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

It works by using android.support.v7.app.ActionBarDrawerToggle, the support.v4 class is deprecated in the meantime. See How to use support-v7-appcompat library

Community
  • 1
  • 1
JanPl
  • 794
  • 6
  • 19