Skip to content

Commit 299a6af

Browse files
committed
* add badge to DrawerItems
1 parent c2a2480 commit 299a6af

File tree

4 files changed

+86
-14
lines changed

4 files changed

+86
-14
lines changed

library/src/main/java/com/mikepenz/materialdrawer/model/PrimaryDrawerItem.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class PrimaryDrawerItem implements IDrawerItem {
2424
private Drawable selectedIcon;
2525
private String name;
2626
private int nameRes = -1;
27+
private String badge;
2728
private boolean enabled = true;
2829

2930
public PrimaryDrawerItem withIdentifier(int identifier) {
@@ -56,6 +57,11 @@ public PrimaryDrawerItem withName(int nameRes) {
5657
return this;
5758
}
5859

60+
public PrimaryDrawerItem withBadge(String badge) {
61+
this.badge = badge;
62+
return this;
63+
}
64+
5965
public PrimaryDrawerItem setEnabled(boolean enabled) {
6066
this.enabled = enabled;
6167
return this;
@@ -81,6 +87,10 @@ public int getNameRes() {
8187
return nameRes;
8288
}
8389

90+
public String getBadge() {
91+
return badge;
92+
}
93+
8494
@Override
8595
public int getIdentifier() {
8696
return identifier;
@@ -113,28 +123,34 @@ public View convertView(Activity activity, LayoutInflater inflater, View convert
113123
}
114124

115125
UIUtils.setBackground(viewHolder.view, UIUtils.getDrawerItemBackground(activity));
126+
116127
if (this.getNameRes() != -1) {
117128
viewHolder.name.setText(this.getNameRes());
118129
} else {
119130
viewHolder.name.setText(this.getName());
120131
}
121132

133+
if (badge != null) {
134+
viewHolder.badge.setText(badge);
135+
viewHolder.badge.setVisibility(View.VISIBLE);
136+
} else {
137+
viewHolder.badge.setVisibility(View.GONE);
138+
}
139+
122140
int color;
123141
int selectedColor = activity.getResources().getColor(R.color.material_drawer_selected_text);
124142
if (this.isEnabled()) {
125143
color = activity.getResources().getColor(R.color.material_drawer_primary_text);
126144
viewHolder.name.setTextColor(UIUtils.getTextColor(color, selectedColor));
145+
viewHolder.badge.setTextColor(UIUtils.getTextColor(color, selectedColor));
127146
} else {
128147
color = activity.getResources().getColor(R.color.material_drawer_hint_text);
129148
viewHolder.name.setTextColor(color);
149+
viewHolder.badge.setTextColor(color);
130150
}
131151

132-
viewHolder.icon.setVisibility(View.VISIBLE);
133-
134152
Drawable icon = null;
135153
Drawable selectedIcon = null;
136-
137-
138154
if (this.getIcon() != null) {
139155
icon = this.getIcon();
140156

@@ -152,23 +168,26 @@ public View convertView(Activity activity, LayoutInflater inflater, View convert
152168
} else {
153169
viewHolder.icon.setImageDrawable(icon);
154170
}
171+
172+
viewHolder.icon.setVisibility(View.VISIBLE);
155173
} else {
156174
viewHolder.icon.setVisibility(View.GONE);
157175
}
158176

159177
return convertView;
160178
}
161179

162-
163180
private class ViewHolder {
164181
private View view;
165182
private ImageView icon;
166183
private TextView name;
184+
private TextView badge;
167185

168186
private ViewHolder(View view) {
169187
this.view = view;
170188
this.icon = (ImageView) view.findViewById(R.id.icon);
171189
this.name = (TextView) view.findViewById(R.id.name);
190+
this.badge = (TextView) view.findViewById(R.id.badge);
172191
}
173192
}
174193
}

library/src/main/java/com/mikepenz/materialdrawer/model/SecondaryDrawerItem.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class SecondaryDrawerItem implements IDrawerItem {
2424
private Drawable selectedIcon;
2525
private String name;
2626
private int nameRes = -1;
27+
private String badge;
2728
private boolean enabled = true;
2829

2930
public SecondaryDrawerItem withIdentifier(int identifier) {
@@ -56,6 +57,11 @@ public SecondaryDrawerItem withName(int nameRes) {
5657
return this;
5758
}
5859

60+
public SecondaryDrawerItem withBadge(String badge) {
61+
this.badge = badge;
62+
return this;
63+
}
64+
5965
public SecondaryDrawerItem setEnabled(boolean enabled) {
6066
this.enabled = enabled;
6167
return this;
@@ -81,6 +87,10 @@ public int getNameRes() {
8187
return nameRes;
8288
}
8389

90+
public String getBadge() {
91+
return badge;
92+
}
93+
8494
@Override
8595
public int getIdentifier() {
8696
return identifier;
@@ -114,27 +124,34 @@ public View convertView(Activity activity, LayoutInflater inflater, View convert
114124
}
115125

116126
UIUtils.setBackground(viewHolder.view, UIUtils.getDrawerItemBackground(activity));
127+
117128
if (this.getNameRes() != -1) {
118129
viewHolder.name.setText(this.getNameRes());
119130
} else {
120131
viewHolder.name.setText(this.getName());
121132
}
122133

134+
if (badge != null) {
135+
viewHolder.badge.setText(badge);
136+
viewHolder.badge.setVisibility(View.VISIBLE);
137+
} else {
138+
viewHolder.badge.setVisibility(View.GONE);
139+
}
140+
123141
int color;
124142
int selectedColor = activity.getResources().getColor(R.color.material_drawer_selected_text);
125143
if (this.isEnabled()) {
126144
color = activity.getResources().getColor(R.color.material_drawer_secondary_text);
127145
viewHolder.name.setTextColor(UIUtils.getTextColor(color, selectedColor));
146+
viewHolder.badge.setTextColor(UIUtils.getTextColor(color, selectedColor));
128147
} else {
129148
color = activity.getResources().getColor(R.color.material_drawer_hint_text);
130149
viewHolder.name.setTextColor(color);
150+
viewHolder.badge.setTextColor(color);
131151
}
132152

133-
viewHolder.icon.setVisibility(View.VISIBLE);
134-
135153
Drawable icon = null;
136154
Drawable selectedIcon = null;
137-
138155
if (this.getIcon() != null) {
139156
icon = this.getIcon();
140157

@@ -152,6 +169,8 @@ public View convertView(Activity activity, LayoutInflater inflater, View convert
152169
} else {
153170
viewHolder.icon.setImageDrawable(icon);
154171
}
172+
173+
viewHolder.icon.setVisibility(View.VISIBLE);
155174
} else {
156175
viewHolder.icon.setVisibility(View.GONE);
157176
}
@@ -163,11 +182,13 @@ private class ViewHolder {
163182
private View view;
164183
private ImageView icon;
165184
private TextView name;
185+
private TextView badge;
166186

167187
private ViewHolder(View view) {
168188
this.view = view;
169189
this.icon = (ImageView) view.findViewById(R.id.icon);
170190
this.name = (TextView) view.findViewById(R.id.name);
191+
this.badge = (TextView) view.findViewById(R.id.badge);
171192
}
172193
}
173194
}

library/src/main/res/layout/drawer_item_primary.xml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="@dimen/material_drawer_primary"
5-
android:paddingRight="@dimen/material_drawer_vertical_padding">
5+
android:paddingLeft="@dimen/material_drawer_vertical_padding"
6+
android:paddingStart="@dimen/material_drawer_vertical_padding"
7+
android:paddingRight="@dimen/material_drawer_vertical_padding"
8+
android:paddingEnd="@dimen/material_drawer_vertical_padding">
69

710
<ImageView
811
android:id="@+id/icon"
@@ -11,18 +14,31 @@
1114
android:layout_marginTop="@dimen/material_drawer_icon_margin"
1215
android:layout_marginRight="@dimen/material_drawer_vertical_padding"
1316
android:layout_marginBottom="@dimen/material_drawer_icon_margin"
14-
android:layout_marginLeft="@dimen/material_drawer_vertical_padding"
1517
android:layout_gravity="center_vertical"/>
1618

19+
<TextView
20+
android:id="@+id/badge"
21+
android:fontFamily="sans-serif"
22+
android:layout_width="wrap_content"
23+
android:layout_height="match_parent"
24+
android:textSize="@dimen/material_drawer_primary_text"
25+
android:gravity="center_vertical|end"
26+
android:textColor="@color/material_drawer_primary_text"
27+
android:layout_alignParentEnd="true"
28+
android:layout_alignParentRight="true"/>
29+
1730
<TextView
1831
android:id="@+id/name"
1932
android:fontFamily="sans-serif"
2033
android:layout_toRightOf="@id/icon"
34+
android:layout_toEndOf="@id/icon"
35+
android:layout_alignRight="@id/badge"
36+
android:layout_alignEnd="@id/badge"
2137
android:layout_width="wrap_content"
2238
android:layout_height="match_parent"
2339
android:layout_marginLeft="@dimen/material_drawer_vertical_padding"
40+
android:layout_marginStart="@dimen/material_drawer_vertical_padding"
2441
android:textSize="@dimen/material_drawer_primary_text"
2542
android:gravity="center_vertical"
26-
android:textAppearance="?android:attr/textAppearanceListItem"
2743
android:textColor="@color/material_drawer_primary_text"/>
2844
</RelativeLayout>

library/src/main/res/layout/drawer_item_secondary.xml

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
android:layout_width="match_parent"
44
android:layout_height="@dimen/material_drawer_secondary"
5-
android:paddingRight="@dimen/material_drawer_vertical_padding">
5+
android:paddingLeft="@dimen/material_drawer_vertical_padding"
6+
android:paddingStart="@dimen/material_drawer_vertical_padding"
7+
android:paddingRight="@dimen/material_drawer_vertical_padding"
8+
android:paddingEnd="@dimen/material_drawer_vertical_padding">
69

710
<ImageView
811
android:id="@+id/icon"
@@ -12,18 +15,31 @@
1215
android:layout_marginTop="@dimen/material_drawer_icon_margin"
1316
android:layout_marginRight="@dimen/material_drawer_vertical_padding"
1417
android:layout_marginBottom="@dimen/material_drawer_icon_margin"
15-
android:layout_marginLeft="@dimen/material_drawer_vertical_padding"
1618
android:layout_gravity="center_vertical"/>
1719

20+
<TextView
21+
android:id="@+id/badge"
22+
android:fontFamily="sans-serif"
23+
android:layout_width="wrap_content"
24+
android:layout_height="match_parent"
25+
android:textSize="@dimen/material_drawer_secondary_text"
26+
android:gravity="center_vertical|end"
27+
android:textColor="@color/material_drawer_secondary_text"
28+
android:layout_alignParentEnd="true"
29+
android:layout_alignParentRight="true"/>
30+
1831
<TextView
1932
android:id="@+id/name"
2033
android:fontFamily="sans-serif"
2134
android:layout_toRightOf="@id/icon"
35+
android:layout_toEndOf="@id/icon"
36+
android:layout_alignRight="@id/badge"
37+
android:layout_alignEnd="@id/badge"
2238
android:layout_width="wrap_content"
2339
android:layout_height="match_parent"
2440
android:layout_marginLeft="@dimen/material_drawer_vertical_padding"
41+
android:layout_marginStart="@dimen/material_drawer_vertical_padding"
2542
android:textSize="@dimen/material_drawer_secondary_text"
2643
android:gravity="center_vertical"
27-
android:textAppearance="?android:attr/textAppearanceListItem"
2844
android:textColor="@color/material_drawer_secondary_text"/>
2945
</RelativeLayout>

0 commit comments

Comments
 (0)