I am developing my own Android Application and i came to a point where i have three different Activities say Activity A, Activity B and Activity C. What i want to do now is to create a Navigation Drawer to navigate between them. I read the tutorial on the Android Developers website but they only focused on Fragments. How are professional Android Applications developed only with one Activity and all the other screens are developed with the use of Fragments?If not why isn't documented how to implement correct the navigation drawer with Activities instead?Thank you for your help.
Asked
Active
Viewed 8,783 times
8
-
1Note that the documentation is saying that you should only use a navigation drawer in case you have more than three top level view. – Karol Babioch May 05 '14 at 15:55
-
1Ok to make it more specific how is the Google Play Store developed in terms of Navigation Drawer? – koufa May 05 '14 at 16:00
2 Answers
16
You need to create a Base activity
which does all the common Drawer navigation
stuff . I will call this base Activity
as DrawerActivity
, and all other Activity
should extend this DrawerActivity
. So all the Activity
will have one instance of Drawer Layout
.
Create a common Layout with DrawerLayout
and place a FrameLayout
and ListView
as child
<android.support.v4.widget.DrawerLayout>
<FrameLayout
android:id="@+id/activity_frame”/>
<ListView
android:id="@+id/left_drawer”/>
</android.support.v4.widget.DrawerLayout>
Now set this Layout in onCreate()
on DrawerActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer_layout);
// do other stuff to initialize drawer layout, add list items
………
……….
// add a listener to the drawer list view
mLeftDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
Add a item click listener
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
switch (position) {
case 0: {
Intent intent = new Intent(DrawerActivity.this, YourActivity.class);
startActivity(intent);
break;
}
default:
break;
}
mDrawerLayout.closeDrawer(mLeftDrawerList);
}
}
Finally, All the other activity will extend this DrawerActivity
public class MainActivity extends DrawerActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// don’t set any content view here, since its already set in DrawerActivity
FrameLayout frameLayout = (FrameLayout)findViewById(R.id.activity_frame);
// inflate the custom activity layout
LayoutInflater layoutInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View activityView = layoutInflater.inflate(R.layout.activity_main, null,false);
// add the custom layout of this activity to frame layout.
frameLayout.addView(activityView);
// now you can do all your other stuffs
}
}
You can see the complete source here https://gist.github.com/libinbensin/613dea436302d3015563

Libin
- 16,967
- 7
- 61
- 83
-
Please provide an complete example of an activity that extends DrawerActivity. I inflated my layout and added that as childview of the frame layout. But it doesn't work. I just need (// add your custom layout of this activity to frame layout.) , how to do that correctly. – ayon May 08 '14 at 06:53
-
See my updated answer. You need to `inflate` your `Activity` layout and add its as a child to the `FrameLayout`. Let me know if you still see any issue – Libin May 09 '14 at 00:34
-
Ahh... I did exactly same code , but I set frameLayout as ViewGroup parameter for inflating the layout. Probably that was the reason why my app seems to fall in infinite loop. I'll try it , if that works it will be a great help.Thank you so much for your help. – ayon May 09 '14 at 03:04
-
-5
You can have a NavigationDrawer in each Activity, populated with the same list of options.

Karakuri
- 38,365
- 12
- 84
- 104