//Create the drawer
result = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.withHasStableIds(true)
.withItemAnimator(new AlphaCrossFadeAnimator())
.withAccountHeader(headerResult) //set the AccountHeader we created earlier for the header
.addDrawerItems(
new PrimaryDrawerItem().withName(R.string.drawer_item_compact_header).withDescription(R.string.drawer_item_compact_header_desc).withIcon(GoogleMaterial.Icon.gmd_sun).withIdentifier(1).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_action_bar_drawer).withDescription(R.string.drawer_item_action_bar_drawer_desc).withIcon(FontAwesome.Icon.faw_home).withIdentifier(2).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_multi_drawer).withDescription(R.string.drawer_item_multi_drawer_desc).withIcon(FontAwesome.Icon.faw_gamepad).withIdentifier(3).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_non_translucent_status_drawer).withDescription(R.string.drawer_item_non_translucent_status_drawer_desc).withIcon(FontAwesome.Icon.faw_eye).withIdentifier(4).withSelectable(false).withBadgeStyle(new BadgeStyle().withTextColor(Color.WHITE).withColorRes(R.color.md_red_700)),
new PrimaryDrawerItem().withName(R.string.drawer_item_advanced_drawer).withDescription(R.string.drawer_item_advanced_drawer_desc).withIcon(GoogleMaterial.Icon.gmd_adb).withIdentifier(5).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_embedded_drawer).withDescription(R.string.drawer_item_embedded_drawer_desc).withIcon(GoogleMaterial.Icon.gmd_battery).withIdentifier(7).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_fullscreen_drawer).withDescription(R.string.drawer_item_fullscreen_drawer_desc).withIcon(GoogleMaterial.Icon.gmd_labels).withIdentifier(8).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_custom_container_drawer).withDescription(R.string.drawer_item_custom_container_drawer_desc).withIcon(GoogleMaterial.Icon.gmd_my_location).withIdentifier(9).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_menu_drawer).withDescription(R.string.drawer_item_menu_drawer_desc).withIcon(GoogleMaterial.Icon.gmd_filter_list).withIdentifier(10).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_mini_drawer).withDescription(R.string.drawer_item_mini_drawer_desc).withIcon(GoogleMaterial.Icon.gmd_battery_charging).withIdentifier(11).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_fragment_drawer).withDescription(R.string.drawer_item_fragment_drawer_desc).withIcon(GoogleMaterial.Icon.gmd_disc_full).withIdentifier(12).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_collapsing_toolbar_drawer).withDescription(R.string.drawer_item_collapsing_toolbar_drawer_desc).withIcon(GoogleMaterial.Icon.gmd_camera_rear).withIdentifier(13).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_persistent_compact_header).withDescription(R.string.drawer_item_persistent_compact_header_desc).withIcon(GoogleMaterial.Icon.gmd_brightness_5).withIdentifier(14).withSelectable(false),
new PrimaryDrawerItem().withName(R.string.drawer_item_crossfade_drawer_layout_drawer).withDescription(R.string.drawer_item_crossfade_drawer_layout_drawer_desc).withIcon(GoogleMaterial.Icon.gmd_format_bold).withIdentifier(15).withSelectable(false),
new SectionDrawerItem().withName(R.string.drawer_item_section_header),
new ExpandableDrawerItem().withName("Collapsable").withIcon(GoogleMaterial.Icon.gmd_collection_case_play).withIdentifier(19).withSelectable(false).withSubItems(
new SecondaryDrawerItem().withName("CollapsableItem").withLevel(2).withIcon(GoogleMaterial.Icon.gmd_8tracks).withIdentifier(2000),
new SecondaryDrawerItem().withName("CollapsableItem 2").withLevel(2).withIcon(GoogleMaterial.Icon.gmd_8tracks).withIdentifier(2001)
),
new SecondaryDrawerItem().withName(R.string.drawer_item_open_source).withIcon(FontAwesome.Icon.faw_github).withIdentifier(20).withSelectable(false),
new SecondaryDrawerItem().withName(R.string.drawer_item_contact).withIcon(GoogleMaterial.Icon.gmd_format_color_fill).withIdentifier(21).withTag("Bullhorn"),
new DividerDrawerItem(),
new SwitchDrawerItem().withName("Switch").withIcon(Octicons.Icon.oct_tools).withChecked(true).withOnCheckedChangeListener(onCheckedChangeListener),
new SwitchDrawerItem().withName("Switch2").withIcon(Octicons.Icon.oct_tools).withChecked(true).withOnCheckedChangeListener(onCheckedChangeListener).withSelectable(false),
new ToggleDrawerItem().withName("Toggle").withIcon(Octicons.Icon.oct_tools).withChecked(true).withOnCheckedChangeListener(onCheckedChangeListener),
new DividerDrawerItem(),
new SecondarySwitchDrawerItem().withName("Secondary switch").withIcon(Octicons.Icon.oct_tools).withChecked(true).withOnCheckedChangeListener(onCheckedChangeListener),
new SecondarySwitchDrawerItem().withName("Secondary Switch2").withIcon(Octicons.Icon.oct_tools).withChecked(true).withOnCheckedChangeListener(onCheckedChangeListener).withSelectable(false),
new SecondaryToggleDrawerItem().withName("Secondary toggle").withIcon(Octicons.Icon.oct_tools).withChecked(true).withOnCheckedChangeListener(onCheckedChangeListener)
) // add the items we want to use with our Drawer
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
//check if the drawerItem is set.
//there are different reasons for the drawerItem to be null
//--> click on the header
//--> click on the footer
//those items don't contain a drawerItem
if (drawerItem != null) {
Intent intent = null;
if (drawerItem.getIdentifier() == 1) {
intent = new Intent(DrawerActivity.this, CompactHeaderDrawerActivity.class);
} else if (drawerItem.getIdentifier() == 2) {
intent = new Intent(DrawerActivity.this, ActionBarActivity.class);
} else if (drawerItem.getIdentifier() == 3) {
intent = new Intent(DrawerActivity.this, MultiDrawerActivity.class);
} else if (drawerItem.getIdentifier() == 4) {
intent = new Intent(DrawerActivity.this, NonTranslucentDrawerActivity.class);
} else if (drawerItem.getIdentifier() == 5) {
intent = new Intent(DrawerActivity.this, AdvancedActivity.class);
} else if (drawerItem.getIdentifier() == 7) {
intent = new Intent(DrawerActivity.this, EmbeddedDrawerActivity.class);
} else if (drawerItem.getIdentifier() == 8) {
intent = new Intent(DrawerActivity.this, FullscreenDrawerActivity.class);
} else if (drawerItem.getIdentifier() == 9) {
intent = new Intent(DrawerActivity.this, CustomContainerActivity.class);
} else if (drawerItem.getIdentifier() == 10) {
intent = new Intent(DrawerActivity.this, MenuDrawerActivity.class);
} else if (drawerItem.getIdentifier() == 11) {
intent = new Intent(DrawerActivity.this, MiniDrawerActivity.class);
} else if (drawerItem.getIdentifier() == 12) {
intent = new Intent(DrawerActivity.this, FragmentActivity.class);
} else if (drawerItem.getIdentifier() == 13) {
intent = new Intent(DrawerActivity.this, CollapsingToolbarActivity.class);
} else if (drawerItem.getIdentifier() == 14) {
intent = new Intent(DrawerActivity.this, PersistentDrawerActivity.class);
} else if (drawerItem.getIdentifier() == 15) {
intent = new Intent(DrawerActivity.this, CrossfadeDrawerLayoutActvitiy.class);
} else if (drawerItem.getIdentifier() == 20) {
intent = new LibsBuilder()
.withFields(R.string.class.getFields())
.withActivityStyle(Libs.ActivityStyle.LIGHT_DARK_TOOLBAR)
.intent(DrawerActivity.this);
}
if (intent != null) {
DrawerActivity.this.startActivity(intent);
}
}
return false;
}
})
.withSavedInstance(savedInstanceState)
.withShowDrawerOnFirstLaunch(true)
.build();
//if you have many different types of DrawerItems you can magically pre-cache those items to get a better scroll performance
//make sure to init the cache after the DrawerBuilder was created as this will first clear the cache to make sure no old elements are in
//RecyclerViewCacheUtil.getInstance().withCacheSize(2).init(result);
new RecyclerViewCacheUtil<IDrawerItem>().withCacheSize(2).apply(result.getRecyclerView(), result.getDrawerItems());
//only set the active selection or active profile if we do not recreate the activity
if (savedInstanceState == null) {
// set the selection to the item with the identifier 11
result.setSelection(21, false);
//set the active profile
headerResult.setActiveProfile(profile3);
}
result.updateBadge(4, new StringHolder(10 + ""));
6 comments
https://github.com/mikepenz/MaterialDrawer
Collapsing toolbar with crossfade, searchbar and a extended actionbutton, plus the latest slider with pinch zoom which I implemented early at dawn before I finally went to sleep. 🙂
I needed to overcome the limitations of a fairly recent obsession: the webview, that I was forced to approach material design beginning with this drawer.
I’m only two weeks in Android development, but I know java for quit a long time and I haven’t tried it for years. I quit java when I realized that there was yet little support for distribution of java enabled phones around 2007 at least in my country, just about the time when Android was yet a company (which I haven’t heard yet back then) and the expense to do it was too high.
But today, Android Studio provides many better promises for android applications than what was then available for mobile devices in Netbeans, even years later, while PHP and javascript was my haven.
There is a good community monitored by Mike himself https://gitter.im/mikepenz/MaterialDrawer you should really check out if there is any problem in using MaterialDrawer library.
It was nice talking to you.