Android: Navigation-Drawer on all activities

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
   }
}

Leave a Comment