Skip to content

Instantly share code, notes, and snippets.

@libinbensin
Last active March 13, 2021 05:30
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save libinbensin/613dea436302d3015563 to your computer and use it in GitHub Desktop.
Save libinbensin/613dea436302d3015563 to your computer and use it in GitHub Desktop.
Android Navigation Drawer with Activities
<?xml version="1.0" encoding="utf-8"?>
<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:background="@color/Red">
<FrameLayout
android:id="@+id/activity_frame"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<ListView
android:id="@+id/left_drawer"
android:layout_height="match_parent"
android:layout_width="240dp"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp"
android:background="@drawable/gray_bg"
android:listSelector="@color/dark_gray_bg"/>
</android.support.v4.widget.DrawerLayout>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="@color/Black"
android:textSize="18sp"
android:minHeight="40dp">
</TextView>
public class DrawerActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout = null;
private ListView mDrawerList = null;
private String[] mDrawerItems;
private ActionBarDrawerToggle mDrawerToggle = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_layout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerItems = getResources().getStringArray(R.array.left_drawer_array);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList.setAdapter(new ArrayAdapter<String>(
this, R.layout.drawer_list_item, mDrawerItems));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this,
mDrawerLayout, R.drawable.ic_drawer,
R.string.drawer_open, R.string.drawer_close) {
public void onDrawerOpened(View view) {
invalidateOptionsMenu();
}
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
for (int index = 0; index < menu.size(); index++) {
MenuItem menuItem = menu.getItem(index);
if (menuItem != null) {
// hide the menu items if the drawer is open
menuItem.setVisible(!drawerOpen);
}
}
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
switch (position) {
case 0: {
Intent intent = new Intent(DrawerActivity.this, SettingsActivity.class);
startActivity(intent);
break;
}
case 1: {
Intent intent = new Intent(DrawerActivity.this, SampleActivity.class);
startActivity(intent);
break;
}
default:
break;
}
mDrawerLayout.closeDrawer(mDrawerList);
}
}
}
<string-array name="left_drawer_array">
<item>Home</item>
<item>Sample</item>
<item>Help</item>
<item>Favorites</item>
<item>Settings</item>
<item>Others</item>
</string-array>
// Sample Activity which extends the DrawerActivity
public class MainActivity extends DrawerActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// don’t set any content view here, since its already set in DrawerActivity
FrameLayout frameLayout = (FrameLayout)findViewById(R.id.activity_frame);
// inflate the custom activity layout
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View activityView = layoutInflater.inflate(R.layout.activity_main, null,false);
// add the custom layout of this activity to frame layout.
frameLayout.addView(activityView);
// now you can do all your other stuffs
}
}
@avnvbhatta
Copy link

Hi, I followed this tutorial but I've ran into a problem. When I create another activity (say ExampleActivity) and extend it, on the onCreate() method, when i set setContentView(R.layout.activity_example.xml), I can see the drawer, but it doesn't have any items on it.

I tried doing what you did on the MainActivity but even if I create a FrameLayout on the activity_example.xml, and follow your code, it gives me a nullpointerexception.

@anukools
Copy link

Thanks @libinbensin for this.. works grt.

@bharathpathan
Copy link

where can i find R.drawer.open and close?

@almakhanov
Copy link

almakhanov commented Aug 3, 2018

Good job and smart approach, but I can't realize the code. What is ActionBarActivity and could you push your full code of your app???

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment