Skip to content

Instantly share code, notes, and snippets.

@kevinvanmierlo
Last active September 1, 2020 15:22
Show Gist options
  • Save kevinvanmierlo/c1a0d6b8bed65b1eeaa6 to your computer and use it in GitHub Desktop.
Save kevinvanmierlo/c1a0d6b8bed65b1eeaa6 to your computer and use it in GitHub Desktop.
Same Navigation Drawer on different Activities
public class BaseActivity extends AppCompatActivity
{
public DrawerLayout drawerLayout;
public ListView drawerList;
private ActionBarDrawerToggle drawerToggle;
protected void onCreate(Bundle savedInstanceState)
{
// R.id.drawer_layout should be in every activity with exactly the same id.
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_drawer, 0, 0)
{
public void onDrawerClosed(View view)
{
getActionBar().setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView)
{
getActionBar().setTitle(R.string.menu);
}
};
drawerLayout.setDrawerListener(drawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
drawerList = (ListView) findViewById(R.id.left_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1,
items)); //Items is an ArrayList or array with the items you want to put in the Navigation Drawer.
drawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
// Do what you want when an item from the Navigation Drawer is clicked
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
}
@Suhailkkhan
Copy link

Now we have the AppCompactActivity and your extending the Activity ;
Im using android.support.v7.widget.RecyclerView as my Drawer and with 10 row items in it
As you told "// R.id.drawer_layout should be in every activity with exactly the same id."
do i need to have "Recyclerview" in every xml , And the All other Implementation To add rows to RecyclerView ???
Thanks in Advance

@kevinvanmierlo
Copy link
Author

@Suhailkkhan You need to have the recyclerview in every xml. But you can make another layout file and use the <include> tag see link. The implementation to add the rows to the RecyclerView can be done in the BaseActivity, so you only have to do it once. Every other Activity needs to extends BaseActivity if you want them to have a NavigationDrawer. Your BaseActivity should indeed extend AppCompatActivity now.

@aswin-jacob-thomas
Copy link

So in the baseactivity where are you setting the contentview ? Actullay we need to put the content view in both base and other activities extending it? Can you please explain. Can you please put the code here.
The setcontent in the base activity is coming on the top of the setcontent xml in the extending activity. So how to set both of them.

@adarsh-tm
Copy link

Hi, I want to include a navigation menu in all of the activities. I used the code "BaseActivity.java" and extended all other activities to BaseActivity. i have an issue, when I try to call one of my activities i get the following runtime exception

Unable to start activity ComponentInfo{lecho.lib.hellocharts.samples.app.wicedsense/lecho.lib.hellocharts.samples.blog_activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.ActionBarDrawerToggle.syncState()' on a null object reference

The following is the code which I modified:

` public class BaseActivity extends AppCompatActivity
{
public DrawerLayout drawerLayout;
public ListView drawerList;
private ActionBarDrawerToggle drawerToggle;
public String[] layers;
private CharSequence mTitle;
private CharSequence mDrawerTitle;

protected void onCreateDrawer(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mTitle =mDrawerTitle=getTitle();
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_drawer, 0, 0)
    {
        public void onDrawerClosed(View view)
        {
            getSupportActionBar().setTitle(mTitle);
            invalidateOptionsMenu();
        }

       public void onDrawerOpened(View drawerView)
        {
            getSupportActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu();
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
    layers = getResources().getStringArray(R.array.nav_drawer_items);
    drawerList = (ListView) findViewById(R.id.list_slidermenu);
    drawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1,
            android.R.id.text1,layers)); //Items is an ArrayList or array with the items you want to put in the Navigation Drawer.

    drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
            // Do what you want when an item from the Navigation Drawer is clicked
        }
    });
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (drawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

}`

I get an exception at this point: drawerLayout.setDrawerListener(drawerToggle);
And i also found out that setting a title was producing an Nullpointer exception getSupportActionBar().setTitle(mTitle); and getSupportActionBar().setTitle(mDrawerTitle);

My question is do I need to hard code the title or is there any other smart way of doing it.

@knapcio
Copy link

knapcio commented May 23, 2016

I get an exception at this point: drawerLayout.setDrawerListener(drawerToggle);

Look at line 9: R.id.drawer_layout should be in every activity with exactly the same id. Your drawerLayout equals null at this point.

@nixoncode
Copy link

Thanks , I Needed This

@AsmaaHassan
Copy link

drawerLayout.setDrawerListener(drawerToggle);
java.lang.NullPointerException

@AsmaaHassan
Copy link

How to color the background of the selected item

@ganeshr-iprogrammer
Copy link

I've followed everything what you given but every activity I am getting bellow error don't know why
"android.view.InflateException: Binary XML file line #28: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference"

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