The "Navigation Drawer" can be opened programmatically with mDrawerLayout.openDrawer(mDrawerContentLayout)
. Here's some info how to set it up:
private DrawerLayout mDrawerLayout;
private RelativeLayout mDrawerContentLayout;
Get the reference to drawerView in your Activity onCreate()
:
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerContentLayout = (RelativeLayout) findViewById(R.id.drawer_content);
Call this on your button click when you want drawer opened:
mDrawerLayout.openDrawer(mDrawerContentLayout);
I used a RelativeLayout
as content view of my drawer, but you can use any which suits you. Just remember, the mDrawerLayout
layout should be inside mDrawerLayout
(which is actually a android.support.v4.widget.DrawerLayout
) and should have the android:layout_gravity="start"
attribute.
Here's the (abbreviated) activity layout that I used:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.view.ViewPager
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
<RelativeLayout
android:id="@+id/drawer_content"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start" >
<ListView
... />
</android.support.v4.widget.DrawerLayout>
You can find more details in the API documentation, the developer docs and the design patterns.