Disabling navigation drawer from fragment
A clean way to do this is to create an interface
that the Activity
implements, through which the Fragment
can call a method local to the Activity
that handles the drawer lock and toggle button states. For example:
public interface DrawerLocker {
public void setDrawerEnabled(boolean enabled);
}
In the Activity
's interface
method, we simply figure the lock mode constant for the DrawerLayout#setDrawerLockMode()
call, and call setDrawerIndicatorEnabled()
on the ActionBarDrawerToggle
.
public class MainActivity extends Activity implements DrawerLocker {
public void setDrawerEnabled(boolean enabled) {
int lockMode = enabled ? DrawerLayout.LOCK_MODE_UNLOCKED :
DrawerLayout.LOCK_MODE_LOCKED_CLOSED;
drawer.setDrawerLockMode(lockMode);
toggle.setDrawerIndicatorEnabled(enabled);
}
...
}
In the Fragment
, we merely need to cast the hosting Activity
to the interface
, and call the setDrawerEnabled()
method accordingly. For example, to lock the drawer shut:
((DrawerLocker) getActivity()).setDrawerEnabled(false);
NB: Since version 23.2.0 of the v7 appcompat support library, ActionBarDrawerToggle
respects the DrawerLayout
's lock mode, and will not toggle the drawer state if it is locked. This means that it is not strictly necessary to use setDrawerIndicatorEnabled()
, though it might be desirable to still do so in order to provide the user a visual indication that the toggle is disabled.
Roshnal
I'm a computer geek, a programmer, love travelling and seek perfection. Enjoys good music, avid movie fan & a food and cocktail lover. Proficient in Java, C++, HTML5/CSS, Javascript, Scala, XML, and about 4 more computer languages. Excellent Android & BlackBerry 10 developer, learning iOS development. Speaks 3 human languages (English, Sinhala, Spanish) and learning 5 more (German, Dutch, Italian, Russian & Swedish). 2-time IOI (International Olympiad in Informatics) participant, 2012 Oracle ThinkQuest runner-up, 3-time NSSC (National School Software Competition) medalist and 2013 IT Mastermind runner-up. (TLDR; I know a lot of languages, program a lot, and won a lot of awards and medals). Find me on Twitter: @roshnal15 :-)
Updated on July 05, 2022Comments
-
Roshnal almost 2 years
I have an app with a navigation drawer and 4 navigation items (Fragments). In one of the Fragments, I have a tab layout set up with a view pager (3 more Fragments).
From one of these inner fragments, I want to disable/enable the navigation drawer dynamically. Basically, on a button press, I want to restrict access to the navigation drawer (and the re-enable on pressing it again).
How would I do it?
I tried accessing the
DrawerLayout
of the parent activity from this inner fragment. But I see no methods to enable/disable the navigation drawer.The way I've added the drawer to my main Activity:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this);
(and of course I've added
toggle.syncState()
from within theonPostCreate
method. -
lcj about 4 yearsThis solution worked, thank you. How do I change the icon so it is not seen. I am having trouble managing the icon in general.
-
Mike M. about 4 yearsAre you looking for
ActionBar#setDisplayHomeAsUpEnabled(false)
maybe? -
Mrityunjai about 3 yearsgetSupportActionBar().setDisplayHomeAsUpEnabled(false); worked for me. Thanks @MikeM.