How to Replace Deprecated Android.Support.V4.App.Actionbardrawertoggle

How to replace deprecated android.support.v4.app.ActionBarDrawerToggle

Adding only android-support-v7-appcompat.jar to library dependencies is not enough, you have also to import in your project the module that you can find in your SDK at the path \android-sdk\extras\android\support\v7\appcompatand after that add module dependencies configuring the project structure in this way

Sample Image

otherwise are included only the class files of support library and the app is not able to load the other resources causing the error.

In addition as reVerse suggested replace this

public CustomActionBarDrawerToggle(Activity mActivity,
DrawerLayout mDrawerLayout) {
super(mActivity, mDrawerLayout,new Toolbar(MyActivity.this) ,
R.string.ns_menu_open, R.string.ns_menu_close);
}

with

public CustomActionBarDrawerToggle(Activity mActivity,
DrawerLayout mDrawerLayout) {
super(mActivity, mDrawerLayout, R.string.ns_menu_open, R.string.ns_menu_close);
}

Deprecated ActionBarDrawerToggle

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

androidx.legacy.app.ActionBarDrawerToggle' is deprecated

Change androidx.legacy.app.ActionBarDrawerToggle to androidx.appcompat.app.ActionBarDrawerToggle, i.e. legacy to appcompat.

Also, remember to add your toolbar as your third argument.

Documentation can be found here:
https://developer.android.com/reference/androidx/appcompat/app/ActionBarDrawerToggle

ActionBarDrawerToggle v7 or v4?

In v7 constructor, you should pass this as parameters :

new ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, int openDrawerContentDescRes, int closeDrawerContentDescRes)

While the v4 constructor is

new ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, int drawerImageRes, int openDrawerContentDescRes, int closeDrawerContentDescRes)

I think in you v7 constructor you kept the drawerImageRes as the third parameter (ic_launcher).

Try with this instead :

new ActionBarDrawerToggle(this, dl, R.string.drawer_open, R.string.drawer_close)

ActionBarDrawerToggle cannot be applied to Android.support.v7.widget.Toolbar

ActionBarDrawerToggle Constructor is as follow.

android.support.v7.app.ActionBarDrawerToggle.ActionBarDrawerToggle(Activity activity,
DrawerLayout drawerLayout,
Toolbar toolbar,
int openDrawerContentDescRes,
int closeDrawerContentDescRes)

You are passing R.drawable.ic_drawer drawable instead of toolbar that's why you are getting this error.

Create a toolbar and add it as action bar and pass this toolbar to this constructor.

Method setDrawerListener is deprecated

Use addDrawerListener() instead.



Related Topics



Leave a reply



Submit