Skip to content

Instantly share code, notes, and snippets.

@Jackyjjc
Created October 8, 2014 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jackyjjc/db0bea2d295481307c9d to your computer and use it in GitHub Desktop.
Save Jackyjjc/db0bea2d295481307c9d to your computer and use it in GitHub Desktop.
Create drawer
/*Add these to MainActivity on create*/
final DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
final ListView drawerList = (ListView) drawerLayout.findViewById(R.id.list_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, new String[]{"Rooms"}));
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
/*you need to write this function, depends on what you want it to do when you click an item on the drawer*/
changeFragment(......................);
drawerList.setItemChecked(pos, true);
drawerLayout.closeDrawer(drawerList);
}
});
mDrawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
R.drawable.ic_drawer,
R.string.abc_action_bar_up_description,
R.string.abc_action_bar_up_description
);
drawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
=====================================================================
Change the MainActivity layout to be
<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">
<FrameLayout
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="comp4920.ping.MainActivity"
tools:ignore="MergeRootFrame" />
<ListView
android:id="@+id/list_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:background="#fff"/>
</android.support.v4.widget.DrawerLayout>
====================================================================
Add the drawer_list_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textAppearance="@android:style/TextAppearance.Medium"
android:minHeight="48dip"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment