19

Have the navigation Drawer working with the sherlock actionbar but i am having trouble getting the 3 line icon (like gmail) to show instead of the normal up button "<". Here is my code ic_drawer is the 3 line icon that gmail uses

getSupportActionBar().setIcon(R.drawable.myIcon);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowTitleEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawer,
            R.drawable.ic_drawer, R.string.menu_open, R.string.menu_close) {
        public void onDrawerClosed(View view) {

            super.onDrawerClosed(view);
        }

        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }
    };
user1634451
  • 5,133
  • 6
  • 30
  • 43

11 Answers11

40

Have you tried implementing the method:

    mDrawerToggle.syncState();

This has worked for me in the past.

wazim
  • 401
  • 1
  • 3
  • 4
  • 2
    I would not have expected this requirement since it works from the base Activity. I agree that this should be marked as a correct answer. – GregM Sep 19 '13 at 22:08
  • 1
    I had to add this in onPostCreate to get the icon showing.. using appcompat thought, not actionbarsherlock. – speedynomads Oct 04 '13 at 16:24
  • worked as solution with abs. Not tested on pre Jelly bean yet though. – Karl Nov 03 '13 at 19:47
  • 2
    This solution does not work on pre-honeycomb devices. The accepted answer does. – Tamás Szincsák Nov 21 '13 at 22:51
  • 1
    It's easy to miss, but the Android documentation actually strongly recommends putting this method in your Activity's `onPostCreate()` method. Note what they say about this [here in the docs](https://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html#syncState()). – Charles Madere Dec 05 '13 at 01:10
  • This is the correct answer if you work with support v4 and not with Actionbar Sherlock. – SjoerdvGestel Feb 27 '14 at 08:56
21

This solution worked for me, and showed default navigation drawer icon in all version. Add SherlockNavigationDrawer library from here https://github.com/nicolasjafelle/SherlockNavigationDrawer to your project. And change your code as below :

SherlockActionBarDrawerToggle mDrawerToggle = new SherlockActionBarDrawerToggle(this,mDrawerLayout,
   R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
   public void onDrawerClosed(View view) {
       super.onDrawerClosed(view);
   }
   public void onDrawerOpened(View drawerView) {
       super.onDrawerOpened(drawerView);
   }
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
Dory
  • 7,462
  • 7
  • 33
  • 55
  • 2
    Did anyone tell you how big a life saver you are?! Was stuck at this problem for the past few hours, until I saw your answer. I wish I could give it a few more upvotes! – Swayam Sep 24 '13 at 20:20
  • 2
    Not a problem at all. And if I may bother you with one small thing, did you notice any weird behaviour in 2.3 devices? Try to press any listItem in the NavigationDrawer and now, drag your finger upwards or downwards(not removing your initial press). Does that select all your listItems and highlights them in white? This does not happen on my 4.3 device(so I am assuming my code is alright). But it happens every time on my 2.3.3 phone. Did you come across anything like this ? – Swayam Sep 25 '13 at 13:23
  • Nevermind. Solved it. Thanks anyway for the initial answer. :) – Swayam Sep 25 '13 at 20:24
  • Hey @Swayam, I tested yesterday in 2.3 and 4.0 devices but i didn't found any such problem in navigation drawer. Anyways your problem is solved till now.But was there any problem in navigation drawer and how did you solved it,may be it could help others. – Dory Sep 26 '13 at 05:25
  • 2
    First of all, thanks for testing on your device for the error. Appreciate it greatly. Anyway, found no more issues and I test my apps extensively before delivering them. So, I guess there are no such compatibility issues. My problem was regarding the listView I had implemented for the navigation drawer. Adding `android:cacheColorHint="@null"` made it go away. It is just weird that nothing of that sort was happening on 4.3. Maybe 4.0+ devices automatically fix that or something. Anyway, won't really call it a problem with the library. – Swayam Sep 26 '13 at 17:20
  • Swayam, wow - I saw exactly the same problem and was mystified. While perhaps not a library bug necessarily, it's a quirk that is worth noting at the least. You saved me a lot of headache with this cacheColorHint solution... thanks! – treejanitor Oct 15 '13 at 17:53
  • In my implementation this answer was 100% correct except for the use of the `setIcon()` method. Calling that with my `R.drawable.*` resource was not necessary and actually caused weird UI problems. Removing that call fixed all issues and the navigation drawer now works fine across all of my test devices. – Charles Madere Dec 05 '13 at 01:12
3

What you can do is create a style like below:

 <style name="AppTheme" parent="Your Theme name">
        <item name="homeAsUpIndicator">@drawable/ic_drawer</item>
 </style>

And in Android manifest apply this theme like:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
</application>

This will solve the issue getting the 3 line icon for sure.

Ansh
  • 2,366
  • 3
  • 31
  • 51
3

Though I'm guessing by now you must have worked it out, just wanted to submit an answer:

Change your code to the following:

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawer,
        R.drawable.ic_drawer, R.string.menu_open, R.string.menu_close) {
    public void onDrawerClosed(View view) {

        super.onDrawerClosed(view);
    }

    public void onDrawerOpened(View drawerView) {
        super.onDrawerOpened(drawerView);
    }
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setIcon(R.drawable.myIcon);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);

Essentially, the ActionBar options that you are setting in code need to be set after the DrawerToggle has been completed, not before.

Raveesh Bhalla
  • 921
  • 8
  • 19
2

You can look at this post first. There is one answer: "You can change back icon in Theme

<item name="android:homeAsUpIndicator">@drawable/action_arrow</item>

But I think you want implement Navigation Drawer, so read about it.

Community
  • 1
  • 1
Borys
  • 1,793
  • 2
  • 17
  • 32
1

Did you add the toggle to your drawer?

mDrawerLayout.setDrawerListener(mDrawerToggle);
Benoit Duffez
  • 11,839
  • 12
  • 77
  • 125
0

This happen when you run your app in Android < 3, even Google apps suffer of this too.

But there is a project than solve this: https://github.com/nicolasjafelle/SherlockNavigationDrawer

Daniel De León
  • 13,196
  • 5
  • 87
  • 72
0

I put this in and it works.

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

@zudo1337 's method solved my problem.

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

I have added this line before calling setDrawerListener.

mDrawerLayout.post(new Runnable() {
@Override public void run() {
 mDrawerToggle.syncState();
}
});

Hope this will fix the issue in lower devices.

KiranKiki
  • 91
  • 2
-2

May be this will work...

in onCreateOptionMenu inflate your menu layout getSupportMenuInflater().inflate(R.menu.action_bar_menu, menu);

user1361425
  • 97
  • 2
  • 7