1

I used this code in my oncreate function to open the navigation drawer by pressing the app icon.

ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                (DrawerLayout) findViewById(R.id.left_drawer), /* DrawerLayout object */
                getResources().getDrawable(R.drawable.ic_drawer),  /* nav drawer icon to replace 'Up' caret */
                getString(R.string.drawer_open),  /* "open drawer" description */
                getString(R.string.drawer_close)  /* "close drawer" description */
                ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(R.string.title_activity_add);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(R.string.drawer_title);
            }
        };

Now it says "The constructor ActionBarDrawerToggle(AddActivity, DrawerLayout, Drawable, String, String) is undefined". I have imported android.support.v4.app.ActionBarDrawerToggle. Where's the problem?

user2971688
  • 123
  • 1
  • 3
  • 9

2 Answers2

4

The constructor is not ActionBarDrawerToggle(AddActivity, DrawerLayout, Drawable, String, String). It is ActionBarDrawerToggle(Activity, DrawerLayout, int, int, int). Change your last three parameters to be the resource IDs, not the results of referencing the resource IDs.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks. I changed the constructor and the parameters and now it says "ActionBarToggle cannot be resolved to a type". I tried importing android.support.v4.app.ActionBarToggle but it didn't help. – user2971688 Nov 09 '13 at 18:55
  • @user2971688: Sorry, the class is `ActionBarDrawerToggle`, not `ActionBarToggle`. I have fixed my answer. I just meant for you to change the last three parameters of your constructor call. – CommonsWare Nov 09 '13 at 19:05
  • Thank you, it seems to work now. Can i ask you another easy question: I want to open the navigation drawer always when the user starts the application. How do i use the openDrawer() function? The id of my navigation drawer is left_drawer. – user2971688 Nov 09 '13 at 19:09
  • @user2971688: Ummm... just call `openDrawer()`, AFAIK. Assuming your drawer is on the left, call `openDrawer(Gravity.LEFT)` on the `DrawerLayout`. – CommonsWare Nov 09 '13 at 19:22
  • When I just call openDrawer(), it says that the method openDrawer() is undefined for the type AddActivity. But what is way more important: The constructor works now, but when I try it on my device, neither the icon is shown, nor can I open the nav drawer by clicking the app icon. This is my code after the consturctor: `mDrawerLayout.setDrawerListener(mDrawerToggle); getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true);` - please also notice that I am new to android development as you can tell probably. – user2971688 Nov 09 '13 at 19:26
  • @user2971688: "it says that the method openDrawer() is undefined for the type AddActivity" -- that is because it is a method on `DrawerLayout`, as I indicated in my comment. If you are new to Android, I would put the navigation drawer aside. It is unlikely that your app needs one (few do), and doing one correctly is rather complicated. – CommonsWare Nov 09 '13 at 19:33
  • @commonsWare hi buddy i just want to ask you that i'm providing ActionBarDrawerToggle to DrawerLayout but the the drawable i'm providing as third parameter it is no visible instead it showing the back button – Asim Habib Jul 07 '14 at 06:22
  • @AsimHabib: I have never tried providing a replacement drawable resource to `ActionBarDrawerToggle`. You may want to ask a fresh Stack Overflow question on this. – CommonsWare Jul 07 '14 at 10:29
0

Just to echo @CommonsWare answer. Instead of findViewById(R.id.left_drawer) just do R.id.left_drawer

So the final result would look like this:

ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                )
Nicolasome
  • 341
  • 1
  • 3
  • 21