Deprecated ActionBarDrawerToggle

Cris picture Cris · Feb 24, 2015 · Viewed 20.2k times · Source

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?

Answer

Cris picture Cris · Apr 16, 2015

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