Method setDrawerListener is deprecated

AndroidDrawerlayoutDeprecation Warning

Android Problem Overview


While I'm doing something on my app, I see that the navigation drawer on my app reduced its size. But I'm not doing anything on that.

navigation drawer

Then, after checking the code, I saw that setDrawerListener is deprecated. Does anyone has a solution to this?

drawerLayout.setDrawerListener(actionBarDrawerToggle);

Android Solutions


Solution 1 - Android

Use addDrawerListener() instead.

Solution 2 - Android

Replace:

drawer.setDrawerListener(...);

with

drawer.addDrawerListener(...);

> public void setDrawerListener(DrawerLayout.DrawerListener listener) Sets a listener to be notified of drawer events.

> Note that this method is deprecated and you should use addDrawerListener(DrawerLayout.DrawerListener) to add a listener and removeDrawerListener(DrawerLayout.DrawerListener) to remove a registered listener.

Solution 3 - Android

Replace setDrawerListener

drawerLayout.setDrawerListener(actionBarDrawerToggle);

with addDrawerListener

drawerLayout.addDrawerListener(actionBarDrawerToggle);

example

  DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            assert drawer != null;
            drawer.addDrawerListener(toggle);
            toggle.syncState();
    
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            assert navigationView != null;
            navigationView.setNavigationItemSelectedListener(this);

Solution 4 - Android

I guess I'm gonna answer my question. The latest navigationView produces its default android:layout_height at almost 18dp when you choose "wrap_content". So, you must choose the android:layout_height that you want for your navigationView or simply make android:layout_height="match_parent".

<android.support.design.widget.NavigationView
    android:layout_width="320dp"
    android:layout_height="550dp"
    android:id="@+id/navigation_view_admin"
    android:layout_gravity="start">

</android.support.design.widget.NavigationView>

Anyways, it's height automatically increases when you add an item in the navigation drawer.

Lastly, Use addDrawerListener() instead of setDrawerListener() as Luxi Liu said.

Solution 5 - Android

In Official Android Developer Documentsenter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiondavid gloriosoView Question on Stackoverflow
Solution 1 - AndroidLuxi LiuView Answer on Stackoverflow
Solution 2 - AndroidJzapataView Answer on Stackoverflow
Solution 3 - AndroidsivaBE35View Answer on Stackoverflow
Solution 4 - Androiddavid gloriosoView Answer on Stackoverflow
Solution 5 - AndroidSajawal BashirView Answer on Stackoverflow