Method setDrawerListener is deprecated
Asked Answered
A

5

230

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);
Appliance answered 25/2, 2016 at 22:14 Comment(4)
The deprecated code probably has nothing to do with the drawer size. It means that they will remove support for it in the future (the specific method). It probably is replaced with an addDrawerListener but I'm not sure on that.Auteur
i see.. but i can't see any problem, I just created a new fragment that is not connected on that drawer.. can't really understand why that thing happened...Appliance
what version of library you are using ?Emetine
I'm using this >> compile 'com.android.support:appcompat-v7:23.2.0' , compile 'com.android.support:design:23.2.0', compile 'com.android.support:support-v4:23.2.0'Appliance
A
763

Use addDrawerListener() instead.

Aksel answered 26/2, 2016 at 4:30 Comment(3)
For API consistency. They added removeDrawerListener() so add... is named to matchWanigan
at least they could included use 'addDrawerListener' instead to deprecated message!Calamite
So... can we add multiple drawer listeners?Fertile
G
87

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.

Gelsenkirchen answered 28/2, 2016 at 3:46 Comment(2)
Where should we remove the drawer listener and is this necessary?Marelda
@Marelda It depends. Generally, it's not necessary. If you set it in onCreate() and you rotate screen whole activity is recreated and new listener is set. More complex cases should be handled with thought that (probably) there is no need to have more than 1 listener set to DrawerLayout.Platitude
U
30

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);
Urania answered 18/5, 2016 at 12:14 Comment(0)
A
22

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.

Appliance answered 27/2, 2016 at 7:6 Comment(2)
because that's not my main question. can't you see? Please read my problem before... It's not only the deprecated thing,Appliance
Then may be fix your question's titleAlvinaalvine
S
0

In Official Android Developer Documentsenter image description here

Skyward answered 3/10, 2021 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.