Can I listen to when the Navigation drawer is opened or closed (e.g: listener like button onclick)?
Asked Answered
H

6

14

I want to call some methods when Navigation Drawer is opened & closed. How can I listen to it (like button's onclick listener) ?

I know we can check (mDrawer.isDrawerOpen(Gravity.LEFT)) or ((mDrawer.isDrawerClose(Gravity.LEFT))).

Thanks in Advance.

Helices answered 14/12, 2015 at 11:11 Comment(0)
E
28

Yeah there are four abstract methods that you can use as listeners

public abstract void onDrawerClosed (View drawerView)
public abstract void onDrawerOpened (View drawerView)
public abstract void onDrawerSlide (View drawerView, float slideOffset)
public abstract void onDrawerStateChanged (int newState)

can be used like this

ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
        R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
    public void onDrawerClosed(View view) {
        super.onDrawerClosed(view);
        // Do whatever you want here
    }
    public void onDrawerOpened(View drawerView) {
        super.onDrawerOpened(drawerView);
        // Do whatever you want here
    }
};
mDrawerLayout.addDrawerListener(mDrawerToggle);

copied from http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.DrawerListener.html

UPDATE setDrawerListener method was deprecated in API level 24.1.0. so use addDrawerListener()

Enthusiastic answered 14/12, 2015 at 11:22 Comment(2)
setDrawerListener is deprecated in API level 23.2.0. use addDrawerListenerWoodchuck
How can I do that in Xamarin?Waggish
C
5

I know this is a very old question, but I had the same question.

for newer android versions you can do the following :

 DrawerLayout drawer = findViewById(R.id.drawer_layout);

 drawer.addDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {

        }

        @Override
        public void onDrawerOpened(@NonNull View drawerView) {
        }

        @Override
        public void onDrawerClosed(@NonNull View drawerView) {

        }

        @Override
        public void onDrawerStateChanged(int newState) {
            Toast.makeText(SideMenuActivity.this, "state: " + newState, Toast.LENGTH_SHORT).show();

        }
    });
Conventual answered 26/4, 2021 at 11:3 Comment(0)
H
3

You can know it by using this piece of code by using ActionBarDrawerToggle as:

     ActionBarDrawerToggle  drawerToggle = new  android.support.v7.app.ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);

        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);

        }
    };
    mDrawer.setDrawerListener(drawerToggle);
Hyderabad answered 14/12, 2015 at 11:18 Comment(1)
How can I do that in Xamarin?Waggish
S
3

There are 4 abstract methods provided in the abstract inner class 'DrawerListener' of 'DrawerLayout' Class

public abstract void onDrawerClosed (View drawerView)
public abstract void onDrawerOpened (View drawerView)
public abstract void onDrawerSlide (View drawerView, float slideOffset)
public abstract void onDrawerStateChanged (int newState)

Useful Example :

((DrawerLayout) findViewById(R.id.drawer_layout)).addDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
            // Whatever you want
        }

        @Override
        public void onDrawerOpened(@NonNull View drawerView) {
            // Whatever you want
        }

        @Override
        public void onDrawerClosed(@NonNull View drawerView) {
            // Whatever you want
        }

        @Override
        public void onDrawerStateChanged(int newState) {
            // Whatever you want
        }
    });

You can check this for more informations HERE

Swirly answered 25/11, 2017 at 11:24 Comment(1)
How can I do that in Xamarin?Waggish
U
1

same issue here and this is what worked for me in Kotlin:

        drawerLayout.addDrawerListener(object : DrawerLayout.DrawerListener {
            override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
                println("Drawer is sliding")
            }

            override fun onDrawerOpened(drawerView: View) {
                println("Drawer is open")
            }

            override fun onDrawerClosed(drawerView: View) {
                println("Drawer is closed")
            }

            override fun onDrawerStateChanged(newState: Int) {
                println("Drawer state changed")
            }
        })
Unreserved answered 1/3, 2023 at 20:33 Comment(0)
L
0

KOTLIN

Initialize the DrawerLayout View

`val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)`

If you want to take actions automatically when the drawer is opened or closed, you can do the following.

  1. In your main activity, create an inner class that is a subclass of DrawerLayout.DrawerListener. The DrawerLayout class implements the DrawerListener interface. As mentioned in this post, the onDrawerSlide function can be used to take an action when the drawer starts to open.

    inner class CustomDrawer : DrawerLayout.DrawerListener{
      override fun onDrawerStateChanged(newState: Int) {
      }
    
      override fun onDrawerSlide(drawerView: View, slideOffset: Float) {
        imm.hideSoftInputFromWindow(drawerView?.getWindowToken(), 0)
      }
    
      override fun onDrawerClosed(drawerView: View) {
        imm.hideSoftInputFromWindow(drawerView?.getWindowToken(), 0)
      }
    
      override fun onDrawerOpened(drawerView: View) {
        imm.hideSoftInputFromWindow(drawerView?.getWindowToken(), 0)
      }
    }
    
  2. Put your action in the function you want to use. In my example, I'm closing the soft keyboard if the user interacts with the navigation drawer. Declare the InputMethodManager like this in your main activity:

    var imm: InputMethodManager = this.getSystemService(Activity.INPUT_METHOD_SERVICE)

  3. Add your custom DrawerListener to the drawerLayout (I put it in the onCreate method)

    var drawerListener = CustomDrawer() drawerLayout.addDrawerListener(drawerListener)

Limb answered 5/2, 2020 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.