-
Notifications
You must be signed in to change notification settings - Fork 433
Set Up a Navigation Drawer Activity
Setting up an Activity is a three step process:
Create a java class with your IDE, then make it extends MaterialNavigationDrawer
public class MyNavigationDrawer extends MaterialNavigationDrawer {
@Override
public void init(Bundle savedInstanceState) {
}
}
MaterialNavigationDrawer
is an ActionBarActivity
, but there are some important changes:
- you have an init method
- you must not override
onCreate
method - you must not call
setContentView
method, because the library have it's own layout - you must not override
onBackPressed
method, because the library implement it on its own
This method is called into the ActionBarActivity onCreate method.
It is used for inserts the drawer list items (called Sections) and Accounts, if you are using the account drawer type.
If you are doing a migration from another navigation drawer, you should put all activity customization here.
N.B. You must have at least one section added to the drawer or it is useless.
Now that your activity class is created, you should choose what type of drawer you want.
Open your styles.xml and add a new theme:
<style name="MyNavigationDrawerTheme" parent="MaterialNavigationDrawerTheme">
<item name="colorPrimary">#8bc34a</item>
<item name="colorPrimaryDark">#558b2f</item>
<item name="colorAccent">#FFFFFF</item>
</style>
All themes should have colorPrimary,colorPrimaryDark and colorAccent
item. They are added by AppCompat library for support all devices, from Froyo to Lollipop.
Like the official android themes, there are three types of the same theme:
- MaterialNavigationDrawer (Dark Navigation Drawer)
- MaterialNavigationDrawer.Light
- MaterialNavigationDrawer.Light.DarkActionBar
For advanced theme customization, please go there: Advanced Theme Customization
The final step, you have to declare into your manifest the activity:
<activity android:name="MyNavigationDrawer" android:theme="@style/MyNavigationDrawerTheme" />
Now you have a working navigation drawer.