Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android: Easiest way to reuse a common Navigation drawer among a group of activities.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:id="@+id/view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/menu_test"
/>
</android.support.v4.widget.DrawerLayout>
/*
* This is a simple and easy approach to reuse the same
* navigation drawer on your other activities. Just create
* a base layout that conains a DrawerLayout, the
* navigation drawer and a FrameLayout to hold your
* content view. All you have to do is to extend your
* activities from this class to set that navigation
* drawer. Happy hacking :)
* P.S: You don't need to declare this Activity in the
* AndroidManifest.xml. This is just a base class.
*/
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
public abstract class AppBaseActivity extends AppCompatActivity implements MenuItem.OnMenuItemClickListener {
private FrameLayout view_stub; //This is the framelayout to keep your content view
private NavigationView navigation_view; // The new navigation view from Android Design Library. Can inflate menu resources. Easy
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private Menu drawerMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.app_base_layout);// The base layout that contains your navigation drawer.
view_stub = (FrameLayout) findViewById(R.id.view_stub);
navigation_view = (NavigationView) findViewById(R.id.navigation_view);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 0, 0);
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawerMenu = navigation_view.getMenu();
for(int i = 0; i < drawerMenu.size(); i++) {
drawerMenu.getItem(i).setOnMenuItemClickListener(this);
}
// and so on...
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
/* Override all setContentView methods to put the content view to the FrameLayout view_stub
* so that, we can make other activity implementations looks like normal activity subclasses.
*/
@Override
public void setContentView(int layoutResID) {
if (view_stub != null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
View stubView = inflater.inflate(layoutResID, view_stub, false);
view_stub.addView(stubView, lp);
}
}
@Override
public void setContentView(View view) {
if (view_stub != null) {
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
view_stub.addView(view, lp);
}
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
if (view_stub != null) {
view_stub.addView(view, params);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
// handle it
break;
case R.id.item2:
// do whatever
break;
// and so on...
}
return false;
}
}
@kolblibrary

This comment has been minimized.

Show comment
Hide comment
@kolblibrary

kolblibrary Nov 26, 2017

you're a God send

you're a God send

@MadCoderr

This comment has been minimized.

Show comment
Hide comment
@MadCoderr

MadCoderr Feb 23, 2018

it's totally awesome. Thanks a lot

it's totally awesome. Thanks a lot

@mTeden

This comment has been minimized.

Show comment
Hide comment
@mTeden

mTeden Feb 27, 2018

totally awersome! thank you so much!

mTeden commented Feb 27, 2018

totally awersome! thank you so much!

@BorisELG

This comment has been minimized.

Show comment
Hide comment
@BorisELG

BorisELG Mar 1, 2018

Good job man !

BorisELG commented Mar 1, 2018

Good job man !

@GTripathee

This comment has been minimized.

Show comment
Hide comment
@GTripathee

GTripathee Mar 26, 2018

Thank god I get here!

Thank god I get here!

@iSalmankhanZ

This comment has been minimized.

Show comment
Hide comment
@iSalmankhanZ

iSalmankhanZ Mar 27, 2018

Thanks!! you are a genius!!

Thanks!! you are a genius!!

@elhayra

This comment has been minimized.

Show comment
Hide comment
@elhayra

elhayra Apr 22, 2018

Thank you. Works great

elhayra commented Apr 22, 2018

Thank you. Works great

@Sookdeo

This comment has been minimized.

Show comment
Hide comment
@Sookdeo

Sookdeo Apr 23, 2018

Hi,
How do you navigate to another activity using onMenuItemClick(MenuItem item). An example would be excellent. Thanks

Sookdeo commented Apr 23, 2018

Hi,
How do you navigate to another activity using onMenuItemClick(MenuItem item). An example would be excellent. Thanks

@habi-kvana

This comment has been minimized.

Show comment
Hide comment
@habi-kvana

habi-kvana May 11, 2018

it's awesome...

it's awesome...

@anwinraj

This comment has been minimized.

Show comment
Hide comment
@anwinraj

anwinraj May 26, 2018

Thanks a lot........

Thanks a lot........

@artemisIrelia

This comment has been minimized.

Show comment
Hide comment
@artemisIrelia

artemisIrelia May 31, 2018

Where do I use the Butterknife.bind() in this type of implementation?

Where do I use the Butterknife.bind() in this type of implementation?

@ziggy192

This comment has been minimized.

Show comment
Hide comment
@ziggy192

ziggy192 Jun 21, 2018

thanks God someone did it

thanks God someone did it

@ALI-KH-Y

This comment has been minimized.

Show comment
Hide comment
@ALI-KH-Y

ALI-KH-Y Jun 25, 2018

I added a toolbar to base. Now how to add three dots menu to this base? I tried to do it by this way, But it doesn't work!

ALI-KH-Y commented Jun 25, 2018

I added a toolbar to base. Now how to add three dots menu to this base? I tried to do it by this way, But it doesn't work!

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