CyberHydra CyberHydra - 2 years ago 252
Android Question

Navigation Drawer to switch between activities

I have browsed the website for a bit and I can't find an answer to my problem, I am trying to get my Navigation Drawer to switch between activities instead of fragments. I have tried switch statements and all that does is crash the app, I don't know how to get the separate elements of the drawer in order to set them up so that if one is pressed, it will go to this page and if the other is pressed it will go to this page etc etc.

Here's my code,

package com.example.ColeraineTown;


public class HomeScreen extends Activity {

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

protected void onCreate(Bundle savedInstanceState) {

// get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.items);
// get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(;

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

// 2. App Icon
drawerLayout = (DrawerLayout) findViewById(;

// 2.1 create ActionBarDrawerToggle
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 */

// 2.2 Set actionBarDrawerToggle as the DrawerListener

// 2.3 enable and show "up" arrow

drawerListView.setOnItemClickListener(new DrawerItemClickListener());

protected void onPostCreate(Bundle savedInstanceState) {
// Sync the toggle state after onRestoreInstanceState has occurred.

public void onConfigurationChanged(Configuration newConfig) {

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 {
public void onItemClick(AdapterView parent, View view, int position, long id) {




I have been at this all day, trying to fix it and get it working, but no luck. It took me so long to get the actual drawer working in the first place it would be a shame to see it all gone.

If you guys have the answer to being able to switch between the activities, that would be great!

Answer Source

Suppose you have 5 items (from 0 index to 4), each index identifying an Activity of your project. You can create a method selectItem(int position)to know what drawer item has been chosen by user.

public void selectItem(int position) {
    Intent intent = null;
    switch(position) {
        case 0:
            intent = new Intent(this, Activity_0.class);
        case 1:
            intent = new Intent(this, Activity_1.class);


        case 4: 
            intent = new Intent(this, Activity_4.class);

        default : 
            intent = new Intent(this, Activity_0.class); // Activity_0 as default


Finally, add this method to your DrawerItemClickListener :

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


It's easier than using Fragments, I think !!!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download