Created
June 11, 2015 06:16
-
-
Save intari/774e4187ba43df34521b to your computer and use it in GitHub Desktop.
Make it possible to use custom centered background resource in MaterialDrawer library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 [email protected] 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