Disabling navigation drawer from fragment
Asked Answered
S

2

20

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 the onPostCreate method.

Spilt answered 27/3, 2016 at 16:10 Comment(5)
Use setDrawerLockMode() for the DrawerLayout, and setDrawerIndicatorEnabled() for the ActionBarDrawerToggle.Illuminance
@MikeM. Setting setDrawerLockMode() stopped the swiping-gesture. But to set setDrawerIndicatorEnabled(), how do I access ActionBarDrawerToggle from within my Fragment?Spilt
Ideally, you should be doing this through an interface that the Activity implements, where the interface's method in the Activity calls those above-mentioned methods.Illuminance
@MikeM. Yep, I tried to do it. But things got very complicated because I'm trying to do this within one of the Fragments in the ViewPager of a Tab layout, which in turn is contained in a Fragment owned by the main activity :-/ If you could provide any hints on doing this (like pseudo-code? or a basic code-structure?), it would be really greatSpilt
"But things got very complicated because I'm trying to do this within one of the Fragments in the ViewPager of a Tab layout, which in turn is contained in a Fragment owned by the main activity" - That's not really relevant, as the Fragment is still attached directly to the Activity. Cast getActivity() to your interface.Illuminance
I
76

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.

Illuminance answered 27/3, 2016 at 17:18 Comment(3)
This solution worked, thank you. How do I change the icon so it is not seen. I am having trouble managing the icon in general.Cagey
Are you looking for ActionBar#setDisplayHomeAsUpEnabled(false) maybe?Illuminance
getSupportActionBar().setDisplayHomeAsUpEnabled(false); worked for me. Thanks @MikeM.Fusil
O
1

for Kotlin language, these two lines: first line is for closing the drawer, second line is for setting its mode to "LOCK_MODE_LOCKED_CLOSED" (to make it disabled)

drawerLayout.close()
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)

"drawerLayout" is defined like this: first line: declaring an instance of a DrawerLayout object second line: assigning a value to it

private lateinit var drawerLayout:DrawerLayout
drawerLayout = findViewById(R.id.activity_main_container)
Onder answered 27/12, 2021 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.