Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 706
Venkatesh Selvam Avatar asked Dec 14 '15 11:12

Venkatesh Selvam


4 Answers

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()

like image 179
Rahul Avatar answered Oct 13 '22 10:10

Rahul


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);
like image 41
Kapil Rajput Avatar answered Oct 13 '22 12:10

Kapil Rajput


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

like image 29
abd3llatif Avatar answered Oct 13 '22 11:10

abd3llatif


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();

        }
    });
like image 36
Joseph Ali Avatar answered Oct 13 '22 12:10

Joseph Ali