user3582433 user3582433 - 3 months ago 19
Java Question

Android: Navigation-Drawer on all activities

I want to add navigation drawer on all actvities of my Android project. This is the code of the MainActivity:

public class MainActivity extends Activity {


private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle actionBarDrawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.items);


// get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);

// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));

// App Icon
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
//drawerLayout = (DrawerLayout) findViewById(R.drawable.ic_drawer_2);

actionBarDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);

// Set actionBarDrawerToggle as the DrawerListener
drawerLayout.setDrawerListener(actionBarDrawerToggle);

getActionBar().setDisplayHomeAsUpEnabled(true);

// just styling option add shadow the right edge of the drawer
drawerLayout.setDrawerShadow(R.drawable.ic_drawer, GravityCompat.START);

drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {

// call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
// then it has handled the app icon touch event
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {

displayView(position);

drawerLayout.closeDrawer(drawerListView);

}

private void displayView(int position)
{
switch (position)
{
case 0:
secondactivity();
break;


case 1:
Toast.makeText(MainActivity.this, "2", Toast.LENGTH_LONG).show();
break;

case 2:
Toast.makeText(MainActivity.this, "3", Toast.LENGTH_LONG).show();

default:
break;
}

}
}

public void secondactivity (){

Intent cambioActivity;

cambioActivity = new Intent (this, SecondActivity.class);

startActivity(cambioActivity);
}
}


In this code I create the navigation drawer, I want the navigation drawer on all activities, so my Second Activity's code is this:

public class SecondActivity extends MainActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondactivity);

}


The navigation drawer is on the first activity but there isn't on other activities, why? Can someone help me?

Answer

The easy way is that you should create fragments. If you are ready to for little hard thing then this is for you. It will let you have same navigation drawer in all activities.

Create drawer_n_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/drawer_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<YourDrawer
    android:id="@+id/drawer_drawer"
    android:layout_width="match_parent"
    android:layout_height="fill_parent" >

</YourDrawer>

</RelativeLayout>

Your DrawerActivity.class

public class DrawerActivity extends Activity {

    public RelativeLayout fullLayout;
    public FrameLayout frameLayout;

    @Override
    public void setContentView(int layoutResID) {

        fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.drawer_n_activity, null);
        frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);

        getLayoutInflater().inflate(layoutResID, frameLayout, true);

        super.setContentView(fullLayout);

        //Your drawer content...

    }
}

Now, to include same Navigation Drawer in all your activities and mind one more thing, all your activities must extend DrawerActivity

public class MainActivity extends DrawerActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); //layout for 1st activity
   }
}

public class SecondActivity extends DrawerActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity); //layout for 2nd activity
   }
}