9

I was trying to implement the android.support.v4.app.ActionBarDrawerToggle in my app; since this class is deprecated

This class is deprecated. Please use ActionBarDrawerToggle in support-v7-appcompat.

I've switched to android.support.v7.app.ActionBarDrawerToggle.

Before I could call the constructor in this way:

mDrawerToggle = new ActionBarDrawerToggle(
    this,                  /* host Activity */
    mDrawerLayout,         /* DrawerLayout object */
    R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
    R.string.drawer_open,  /* "open drawer" description for accessibility */
    R.string.drawer_close  /* "close drawer" description for accessibility */
){
    public void onDrawerClosed(View view) {
        getActionBar().setTitle(mTitle);
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
    }

    public void onDrawerOpened(View drawerView) {
        getActionBar().setTitle(mDrawerTitle);
        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
    }
};

but after I've switched to the newer v7 support library, I receive the error

"ActionBarDrawerToggle() in ActionBarDrawerToggle cannot be applied to:
toolbar: android.support.v7.widget.Toolbar 
Actual arguments: R.drawable.ic_drawer (int)"

Apparently I am not introducing a proper Toolbar into the constructor, but I'm not sure to understand the difference between the two conflicting arguments. How do I get the required toolbar?

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81
Cris
  • 2,002
  • 4
  • 30
  • 51
  • 1
    I second this question. I found this as a partial solution: getActionBar().setDisplayShowHomeEnabled(true); getActionBar().setIcon(R.drawable.ic_drawer); The problem is that the icon has padding - how can I remove that? Also - where can I find a good ic_drawer.png? – mikeesouth Mar 11 '15 at 22:54
  • 1
    In the mean time this question was answered in http://stackoverflow.com/questions/26439619/how-to-replace-deprecated-android-support-v4-app-actionbardrawertoggle – k3b Apr 14 '15 at 19:12

1 Answers1

20

I solved my problem by importing the newer android.support.v7.app.ActionBarDrawerToggle and by using the RecyclerView instead of the ListView as shown in this example: How to make Material Design Navigation Drawer With Header View:

private ActionBarDrawerToggle mDrawerToggle;
//... ... 
mDrawerToggle = new ActionBarDrawerToggle(
            this,
            mDrawerLayout,
            toolbar,
            R.string.drawer_open, R.string.drawer_close){
                    @Override
                    public void onDrawerOpened(View drawerView) {
                        super.onDrawerOpened(drawerView);
                        // code here will execute once the drawer is opened 
                        getSupportActionBar().setTitle(mTitle);
                        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                    }
                    @Override
                    public void onDrawerClosed(View drawerView) {
                        super.onDrawerClosed(drawerView);
                        // Code here will execute once drawer is closed
                        getSupportActionBar().setTitle(mDrawerTitle);
                        invalidateOptionsMenu();
    };

If you still have trouble check here: How to replace deprecated android.support.v4.app.ActionBarDrawerToggle

Community
  • 1
  • 1
Cris
  • 2,002
  • 4
  • 30
  • 51