So I have tabs that I want to hide when the Navigation Drawer starts opening. The code I have hides them when it finished opening, but it's not what I want.
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
@Override
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
setActionBarMode(ActionBar.NAVIGATION_MODE_TABS);
}
@Override
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
setActionBarMode(ActionBar.NAVIGATION_MODE_STANDARD);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
Here's what I tried:
- Setting an
onClickListener
tomDrawerLayout
.onClick
never gets called - Setting an
onTouchListener
tomDrawerLayout
.onTouch
never gets called - Researched
ActionBarDrawerToggle
andDrawerLayout
classes. Could not find anything likeonDrawerStartedOpening
.
try to override a method of DrawerLayout.DrawerListener
For Kotlin
It's the best way.
fookwood answer did not work for me but slight modification in the if statment did the trick)
Currently accepted answer by Pavel Dudka is already deprecated. Please use
mDrawerLayout.addDrawerListener()
method instead to set a listener.Works perfectly. Cheers!
Up-to-date solution:
As others have suggested, the current answer is outdated and it's advised to use
mDrawerLayout.addDrawerListener()
. A working solution would then be:Naturally, replace
GravityCompat.START
with whatever identifies your drawer (layout ID or its gravity ~ location).Also, if you want to detect when the drawer starts closing, you can simply do: