22

For Sliding menu by jfeinstein10 (https://github.com/jfeinstein10/SlidingMenu), I can slide anywhere in the app to slide open the menu.

For Google newly introduced navigation drawer http://developer.android.com/design/patterns/navigation-drawer.html#side-nav, is there any way I can have similar behaviour?

So far, from documentation, I saw it only limit to slide from edge or touching the app icon.

The user can bring the navigation drawer onto the screen by swiping from the left edge of the screen or by touching the application icon on the action bar.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
  • trying to do the same thing..I would like to set a margin that could be dragged to open, similar to SlidingMenu. I posted here http://stackoverflow.com/questions/16988597/set-drag-margin-for-android-navigation-drawer – Patrick Jackson Jun 07 '13 at 16:14

5 Answers5

11

Google, from the way they word their tutorial "If the user touches the very left edge of the screen (within 20 dp from the left)", seems like they don't want that functionality.

See http://developer.android.com/design/patterns/navigation-drawer.html

"The user can bring the navigation drawer onto the screen by swiping from the left edge of the screen or by touching the application icon on the action bar."

They do not say or swipe from anywhere on the screen. They also do not have that functionality in any of their apps (G+, Gmail, etc.), so if you want that functionality you should probably stick with writing your own (with gestures) or third party (eg. jfeinstein10).

EDIT: Youtube app does let you swipe anywhere but the version I have at least (4.5.17) doesn't look like it is using their new api.

Cheney Hester
  • 528
  • 1
  • 5
  • 22
  • 4
    This. Now that there is an official design pattern, it is best to follow it. Users will get accustomed to the official way to handle navigation drawers and will expect all apps to follow it. – Teovald Jun 17 '13 at 16:32
11

I have found a solution. You can configure margin of touch and make it as wide as your view is. Here is the link

Set drag margin for Android Navigation Drawer

Community
  • 1
  • 1
Maxim Efimov
  • 2,747
  • 1
  • 19
  • 25
  • 1
    hack don't really work especially when its content is ViewPager. When swiping toward left, with NavigationDrawer, ViewPager won't change from view 0 to view 1. All the events are consumed by NavigationDrawer. – Cheok Yan Cheng Feb 16 '14 at 10:12
6

You can use this

DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
Field mDragger = mDrawerLayout.getClass().getDeclaredField(
    "mLeftDragger");//mRightDragger or mLeftDragger based on Drawer Gravity
mDragger.setAccessible(true);
ViewDragHelper draggerObj = (ViewDragHelper) mDragger
    .get(mDrawerLayout);

Field mEdgeSize = draggerObj.getClass().getDeclaredField(
    "mEdgeSize");
mEdgeSize.setAccessible(true);
int edge = mEdgeSize.getInt(draggerObj);

mEdgeSize.setInt(draggerObj, edge * 3); 
moallemi
  • 2,448
  • 2
  • 25
  • 28
3

As others have said, and Cheney has said in his answer - it's probably best to leave it as intended. However, the DrawerLayout is a different style than the SlidingMenu. Google also added SlidingPaneLayout which matches the style of SlidingMenu more closely.

I ended up implementing a SlidingPaneLayout in this way, as it was more of what I was looking for after all. (This is also the style of the YouTube/Hangouts app)

<android.support.v4.widget.SlidingPaneLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/sliding_pane_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
            android:id="@+id/left_pane"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</android.support.v4.widget.SlidingPaneLayout>

Then to open with the action bar home button:

getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action buttons
    switch(item.getItemId()) {
        case android.R.id.home:
            if (mPaneLayout.isOpen())
                mPaneLayout.closePane();
            else
                mPaneLayout.openPane();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

You can then implement a PanelSlideListener to handle when it sliding/open/closed.

I suggest reading Adam Powell's series on the navigation drawer too - part 3 gets into usage of SlidingPaneLayout vs Navigation Drawer:

Part 1 - https://plus.google.com/+AdamWPowell/posts/2zi4DXd3jkm

Part 2 - https://plus.google.com/+AdamWPowell/posts/VdgexsZeXHW

Part 3 - https://plus.google.com/+AdamWPowell/posts/8j2GVw72i1E

Part 4 - https://plus.google.com/+AdamWPowell/posts/TtBFUXhe5HU

bbedward
  • 6,368
  • 7
  • 36
  • 59
2

You could use a GestureDetector to detect sliding gestures yourself and simply open the Navigation Drawer yourself using the DrawerLayout.openDrawer() method.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • 1
    Yea I thought about this, but this isn't ideal. Instead of the gradual drag that follows your finger, it simply will just open the drawer straight away. Ideally would like the same behavior as default (Otherwise I'll likely go for something like the SlidingMenu implementation) – bbedward Jun 11 '13 at 20:24
  • 1
    As Google finally came up with a standard solution for this kind of navigational element, I think it would - at least for the sake of consistency - be the best to use it as intended by it's makers. If you really need your app to behave in a different way you should probably use SlidingMenu or the like, instead of trying to bend DrawerLayout in a way it isn't designed for. – Ridcully Jun 12 '13 at 13:10
  • Sounds about right, looks like a SlidingPaneLayout may be more of what I'm looking for. – bbedward Jun 12 '13 at 15:26