androidmaterialdrawer

How can I add a header to Mikepenz Material Drawer


I was using mikepenz material drawer library with primary and secondary drawer items and was not able to add header in the material drawer. I want to add a material drawer header (with name and profile image) instead of primary drawer item.

Following below is my code

new DrawerBuilder().withActivity(this).build();



//primary items
PrimaryDrawerItem profile = new PrimaryDrawerItem()
        .withIdentifier(1)
        .withName("John Doe")
        .withDescription("Edit Profile")
        .withDescriptionTextColorRes(R.color.black_overlay)
        .withIcon(R.drawable.avatar);


SecondaryDrawerItem home = (SecondaryDrawerItem) new SecondaryDrawerItem()
        .withIdentifier(13)
        .withName(R.string.drawer_item_home)
        .withIcon(R.drawable.ic_home);
SecondaryDrawerItem play = (SecondaryDrawerItem) new SecondaryDrawerItem()
        .withIdentifier(14)
        .withName(R.string.drawer_item_play)
        .withIcon(R.drawable.ic_play);
SecondaryDrawerItem custom = (SecondaryDrawerItem) new SecondaryDrawerItem()
        .withIdentifier(15)
        .withName(R.string.drawer_item_custom)
        .withIcon(R.drawable.ic_custom);

SecondaryDrawerItem settings = (SecondaryDrawerItem) new SecondaryDrawerItem()
        .withIdentifier(97)
        .withName(R.string.drawer_item_settings)
        .withIcon(R.drawable.ic_setting);
SecondaryDrawerItem about = (SecondaryDrawerItem) new SecondaryDrawerItem()
        .withIdentifier(98)
        .withName(R.string.help)
        .withIcon(R.drawable.ic_help);
SecondaryDrawerItem logout = (SecondaryDrawerItem) new SecondaryDrawerItem()
        .withIdentifier(99)
        .withName(R.string.drawer_item_logout)
        .withIcon(R.drawable.ic_logout);


new DrawerBuilder()
        .withActivity(this)
        .withToolbar(toolbar)
        .withActionBarDrawerToggleAnimated(true)
        .withTranslucentStatusBar(true)
        .withFullscreen(true)
        .withSavedInstance(savedInstanceState)
        .addDrawerItems(
                profile,
                new SectionDrawerItem(),
                home,
                play,
                custom,
                new DividerDrawerItem(),
                settings,
                help,
                logout

        )
        .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
            @Override
            public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {

                return false;
            }
        })
        .build();

I tried to put this instead of the primary drawer item, but I get an error (please pass an activity)

AccountHeader headerResult = new AccountHeaderBuilder()
        .addProfiles(
                new ProfileDrawerItem().withName("John Doe").withEmail("Johndoe@email.com").withIcon(R.drawable.avatar)
    ).withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
        @Override
        public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
            return false;
        }
    }).build();

Can anyone tell me what I'm missing ? please.


Solution

    1. remove this new DrawerBuilder().withActivity(this).build();
    2. Now you should create the account header before setting the drawer (for example below the action bar call)
    3. in the DrawerBuilder method you should add .withAccountHeader(headerResult)