Navigation drawer - Disable click through on items behind the drawer
Asked Answered
F

6

59

Is there any way to make sure that the navigation drawer stays on top of the content in the fragment?

I created a small test application with dummy data. 10 fragments with a corresponding numbered button and textview. The issue is with the fact that the fragment elements seem to have higher priority than the navigation drawer.

enter image description here

As seen in the screenshot, once I attempt to open up the "0 fragment" it instead opts to register the click on the button behind the navigation drawer. Pressing any other content item works fine, but this is as long as there are no other interactable items beneath them. What can I do to have the navigation drawer properly stay on top of the content behind it?

Fiddle answered 6/7, 2013 at 0:36 Comment(1)
Did you try set actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); ?Ambrosius
O
138

Set android:clickable="true" tag on sliding pane layout.

Offish answered 10/10, 2013 at 22:33 Comment(2)
I am using a LinearLayout in a Fragment as my drawer and I added this attribute to the LinearLayout and it worked. Leaving this here so people know this works with something else other than a "sliding pane layout"Flatt
Since the layout is now clickable, a ripple effect is started when you tap it. Can this be disabled?Hanna
P
4

The problem seem not because of click focus,

Visit https://developer.android.com/training/implementing-navigation/nav-drawer.html#DrawerLayout

The main content view (the FrameLayout above) must be the first child in the DrawerLayout because the XML order implies z-ordering and the drawer must be on top of the content.

Porringer answered 23/9, 2015 at 9:32 Comment(1)
What exactly are you trying to tell me with this answer? Just the order of the views in the main layout? If so please write in that way too.Superstratum
E
3

In my fragment drawer, I set TouchListener to return True. It worked for me

        mFragmentContainerView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
Eastsoutheast answered 6/11, 2015 at 4:31 Comment(0)
W
2

I solved it in a different way.

Here is my code for setting up the Drawer:

/**
 * Setup Navigation Drawer
 */
private void setDrawer() {
    NavigationDrawerFragment mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.fragment_drawer);
    mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
}

the setup method is inside my NavigationDrawerFragment, here is my code for it:

/**
 * Users of this fragment must call this method to set up the navigation drawer interactions.
 *
 * @param fragmentId   The android:id of this fragment in its activity's layout.
 * @param drawerLayout The DrawerLayout containing this fragment's UI.
 * @param toolbar      The Toolbar of the activity.
 */
public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
    View mFragmentContainerView = (View) getActivity().findViewById(fragmentId).getParent();
    DrawerLayout mDrawerLayout = drawerLayout;

    //noinspection deprecation
    mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));

    ActionBarDrawerToggle mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, "Drawer opened", "Drawer closed") {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if (!isAdded()) return;

            // Solution:
            // Disable click event on views below Navigation Drawer
            mFragmentContainerView.setClickable(false);
            getActivity().invalidateOptionsMenu();
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if (!isAdded()) return;

            // Solution:
            // Enable click event on views below Navigation Drawer
            mFragmentContainerView.setClickable(true);
            getActivity().invalidateOptionsMenu();
        }
    };

    // Defer code dependent on restoration of previous instance state.
    mDrawerLayout.post(new Runnable() {
        @Override
        public void run() {
            mActionBarDrawerToggle.syncState();
        }
    });

    //noinspection deprecation
    mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
}

That's it

Wheelock answered 19/10, 2016 at 12:13 Comment(0)
S
2

You have to set clickable, focusable and focusableInTouchMode in the highest view of your drawer's layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true"
    android:focusableInTouchMode="true">
Sabba answered 12/2, 2020 at 5:36 Comment(0)
F
-1

Well, I found one solution for this issue. When the drawer is being opened you can bring the nav bar to the front by calling the bringToFront()-method on the layout you use. This makes sure the navigation drawer stays on top of any underlying content until a new item has been selected.

I.e:

@Override
public void onDrawerOpened(View drawerView)
   {
   activity.getActionBar().setTitle("Select content");
   activity.invalidateOptionsMenu();
   drawerLayout.bringToFront();
   }
Fiddle answered 6/7, 2013 at 1:8 Comment(1)
android:clickable="true" on the drawer layout is enoughDysphemia

© 2022 - 2024 — McMap. All rights reserved.