Skip to content

Instantly share code, notes, and snippets.

@intari
Created June 11, 2015 06:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intari/774e4187ba43df34521b to your computer and use it in GitHub Desktop.
Save intari/774e4187ba43df34521b to your computer and use it in GitHub Desktop.
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 dmitriy.kazimirov@viorsan.com 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
*/
@Override
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(R.id.badge);
badge.setBackgroundResource(badgeBackgroundRes);
//put it into correct place
RelativeLayout.LayoutParams lp=(RelativeLayout.LayoutParams)badge.getLayoutParams();
lp.width=convertView.getResources().getDimensionPixelSize(R.dimen.navigation_drawer_badge_width);
lp.height=convertView.getResources().getDimensionPixelSize(R.dimen.navigation_drawer_badge_height);
lp.addRule(RelativeLayout.CENTER_VERTICAL);
badge.setLayoutParams(lp);
//fix position
badge.setGravity(Gravity.CENTER);
}
return convertView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment