android – Touch anywhere to slide open menu for navigation drawer-ThrowExceptions

Exception or error:

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.

How to solve:

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

###

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.

###

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); 

###

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

###

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

Leave a Reply

Your email address will not be published. Required fields are marked *