DrawerLayout
DrawerLayout is drawer which are supported by extension library.
Prepare
You need to import android-support-v4.jar
Toggle action is in Action bar action.(with Actionbar)
Layout
This is xml layout
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context="jp.co.rakuten.toolbar.android.fragment.MainFragment">
<!-- The main content view -->
<RelativeLayout
android:id="@+id/left_draw"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- Also can add fragment here
<fragment
android:id="@+id/action_list"
class="jp.co.rakuten.toolbar.fragment.RewardActionListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="55dp">
</fragment>
-->
</RelativeLayout>
<!-- The navigation drawer -->
<!-- Left part -->
<LinearLayout
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="@color/white" >
<!-- You can add fragment here
<fragment
android:id="@+id/menu_list_fragment"
class="jp.co.rakuten.toolbar.fragment.RewardMenuFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
</fragment>
-->
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Codes
R.drawable.ic_drawer is image resource. You can download. Google Developers
public class MainActivity {
private ActionBarDrawerToggle drawerToggle;
private DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this,
drawer,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close);
drawer.setDrawerListener(drawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true); // This API is over 14(4.0)
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
}
