Skip to content

Instantly share code, notes, and snippets.

Embed
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 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
You can’t perform that action at this time.