Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Make it possible to use custom centered background resource in MaterialDrawer library
package com.viorsan.gists.example1
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
* Created by Dmitriy Kazimirov, e-mail on 10.06.15.
* PrimaryDrawerItem which correctly can display badge in circle which is correctly centered
* directly access PrimaryDrawerItem's inner elements
public class DrawerItemWithBadgeInCircle extends PrimaryDrawerItem {
private int badgeBackgroundRes = -1;
public DrawerItemWithBadgeInCircle withBadgeBackgroundRes(int backgroundRes) {
this.badgeBackgroundRes = backgroundRes;
return this;
* Update badge to use circle background using our knowledge of PrimaryDriverItem's inner structure
* @param inflater
* @param convertView
* @param parent
* @return
public View convertView(LayoutInflater inflater, View convertView, ViewGroup parent) {
//use the logic of regular PrimaryDrawerItem
convertView = super.convertView(inflater, convertView, parent);
if (badgeBackgroundRes!=-1) {
//add configure background resource
TextView badge=(TextView)convertView.findViewById(;
//put it into correct place
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)badge.getLayoutParams();
//fix position
return convertView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.