Skip to content

Commit b5a78bb

Browse files
committed
setup bootstrapbadge to allow any text on badge, not just ints
1 parent a57aae3 commit b5a78bb

10 files changed

Lines changed: 164 additions & 132 deletions

File tree

AndroidBootstrap/src/main/java/com/beardedhen/androidbootstrap/BootstrapBadge.java

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.beardedhen.androidbootstrap.api.attributes.BootstrapBrand;
1111
import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapBrand;
1212
import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapSize;
13+
import com.beardedhen.androidbootstrap.api.view.BootstrapBadgeView;
1314
import com.beardedhen.androidbootstrap.api.view.BootstrapBrandView;
1415
import com.beardedhen.androidbootstrap.api.view.BootstrapSizeView;
1516
import com.beardedhen.androidbootstrap.utils.DimenUtils;
@@ -18,11 +19,11 @@
1819
/**
1920
* See <a href="http://getbootstrap.com/components/#badges>http://getbootstrap.com/components/#badges</a>
2021
*/
21-
public class BootstrapBadge extends ImageView implements BootstrapSizeView, BootstrapBrandView {
22+
public class BootstrapBadge extends ImageView implements BootstrapSizeView, BootstrapBrandView,
23+
BootstrapBadgeView {
2224

23-
private int badgeCount;
25+
private String badgeText;
2426
private int size;
25-
private boolean allowZeroValue;
2627
private boolean insideContainer;
2728
private BootstrapBrand bootstrapBrand = DefaultBootstrapBrand.REGULAR;
2829
private float bootstrapSize;
@@ -49,56 +50,44 @@ private void init(AttributeSet attrs) {
4950
try {
5051
int sizeOrdinal = a.getInt(R.styleable.BootstrapBadge_bootstrapSize, -1);
5152

52-
allowZeroValue = a.getBoolean(R.styleable.BootstrapBadge_allowZeroValue, false);
53+
if (badgeText == null) {
54+
badgeText = a.getString(R.styleable.BootstrapBadge_badgeText);
55+
}
56+
5357
bootstrapSize = DefaultBootstrapSize.fromAttributeValue(sizeOrdinal)
5458
.scaleFactor();
5559
}
5660
finally {
5761
a.recycle();
5862
}
5963

60-
size = (int) DimenUtils.pixelsFromDpResource(getContext(),
61-
R.dimen.bootstrap_badge_default_size);
64+
size = (int) DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_badge_default_size);
6265
updateBootstrapState();
6366
}
6467

6568
private void updateBootstrapState() {
6669
badgeDrawable = BootstrapDrawableFactory.createBadgeDrawable(getContext(), bootstrapBrand,
6770
(int) (size * bootstrapSize),
6871
(int) (size * bootstrapSize),
69-
badgeCount, allowZeroValue,
72+
badgeText,
7073
insideContainer);
7174

7275
ViewUtils.setBackgroundDrawable(this, badgeDrawable);
7376
}
7477

75-
public void setBadgeCount(int badgeCount) {
76-
this.badgeCount = badgeCount;
77-
updateBootstrapState();
78+
Drawable getBadgeDrawable() {
79+
return badgeDrawable;
7880
}
7981

80-
public boolean isAllowZeroValue() {
81-
return allowZeroValue;
82+
@Override public String getBadgeText() {
83+
return badgeText;
8284
}
8385

84-
public void setAllowZeroValue(boolean allowZeroValue) {
85-
this.allowZeroValue = allowZeroValue;
86+
@Override public void setBadgeText(String badgeText) {
87+
this.badgeText = badgeText;
8688
updateBootstrapState();
8789
}
8890

89-
public int getBadgeCount() {
90-
return badgeCount;
91-
}
92-
93-
public Drawable getBadgeDrawable() {
94-
return badgeDrawable;
95-
}
96-
97-
98-
99-
100-
101-
10291
public void setBootstrapBrand(BootstrapBrand bootstrapBrand, boolean insideContainer) {
10392
this.insideContainer = insideContainer;
10493
setBootstrapBrand(bootstrapBrand);

AndroidBootstrap/src/main/java/com/beardedhen/androidbootstrap/BootstrapButton.java

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.os.Bundle;
77
import android.os.Parcelable;
88
import android.support.annotation.NonNull;
9+
import android.support.annotation.Nullable;
910
import android.util.AttributeSet;
1011
import android.view.MotionEvent;
1112
import android.view.ViewParent;
@@ -15,6 +16,7 @@
1516
import com.beardedhen.androidbootstrap.api.defaults.ButtonMode;
1617
import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapSize;
1718
import com.beardedhen.androidbootstrap.api.view.BadgeContainerView;
19+
import com.beardedhen.androidbootstrap.api.view.BootstrapBadgeView;
1820
import com.beardedhen.androidbootstrap.api.view.BootstrapSizeView;
1921
import com.beardedhen.androidbootstrap.api.view.ButtonModeView;
2022
import com.beardedhen.androidbootstrap.api.view.OutlineableView;
@@ -30,7 +32,8 @@
3032
* allowing the use of different selection modes e.g. Checkbox/Radio group.
3133
*/
3234
public class BootstrapButton extends AwesomeTextView implements BootstrapSizeView,
33-
OutlineableView, RoundableView, ButtonModeView, BadgeContainerView {
35+
OutlineableView, RoundableView, ButtonModeView, BadgeContainerView,
36+
BootstrapBadgeView {
3437

3538
private static final String TAG = "com.beardedhen.androidbootstrap.BootstrapButton";
3639
private static final String KEY_MODE = "com.beardedhen.androidbootstrap.BootstrapButton.MODE";
@@ -52,6 +55,7 @@ public class BootstrapButton extends AwesomeTextView implements BootstrapSizeVie
5255
private float baselineStrokeWidth;
5356
private float baselineCornerRadius;
5457
private BootstrapBadge bootstrapBadge;
58+
private String badgeText;
5559

5660
public BootstrapButton(Context context) {
5761
super(context);
@@ -76,6 +80,7 @@ private void initialise(AttributeSet attrs) {
7680
this.roundedCorners = a.getBoolean(R.styleable.BootstrapButton_roundedCorners, false);
7781
this.showOutline = a.getBoolean(R.styleable.BootstrapButton_showOutline, false);
7882
this.mustBeSelected = a.getBoolean(R.styleable.BootstrapButton_checked, false);
83+
this.badgeText = a.getString(R.styleable.BootstrapButton_badgeText);
7984

8085
int sizeOrdinal = a.getInt(R.styleable.BootstrapButton_bootstrapSize, -1);
8186
int modeOrdinal = a.getInt(R.styleable.BootstrapButtonGroup_buttonMode, -1);
@@ -92,7 +97,13 @@ private void initialise(AttributeSet attrs) {
9297
baselineHoriPadding = DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_button_default_hori_padding);
9398
baselineStrokeWidth = DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_button_default_edge_width);
9499
baselineCornerRadius = DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_button_default_corner_radius);
100+
95101
updateBootstrapState();
102+
103+
if (badgeText != null) {
104+
setBadge(new BootstrapBadge(getContext()));
105+
setBadgeText(badgeText);
106+
}
96107
}
97108

98109
@Override public Parcelable onSaveInstanceState() {
@@ -106,8 +117,8 @@ private void initialise(AttributeSet attrs) {
106117
bundle.putSerializable(KEY_MODE, buttonMode);
107118

108119
if (bootstrapBadge != null) {
109-
bundle.putInt(BadgeContainerView.KEY, bootstrapBadge
110-
.getBadgeCount());
120+
bundle.putString(BadgeContainerView.KEY, bootstrapBadge
121+
.getBadgeText());
111122
}
112123
return bundle;
113124
}
@@ -120,7 +131,10 @@ private void initialise(AttributeSet attrs) {
120131
this.showOutline = bundle.getBoolean(OutlineableView.KEY);
121132
this.parentIndex = bundle.getInt(KEY_INDEX);
122133
this.bootstrapSize = bundle.getFloat(BootstrapSizeView.KEY);
123-
if (bootstrapBadge != null) setBadgeCount(bundle.getInt(BadgeContainerView.KEY));
134+
135+
if (bootstrapBadge != null) {
136+
setBadgeText(bundle.getString(BadgeContainerView.KEY));
137+
}
124138

125139
Serializable m = bundle.getSerializable(KEY_MODE);
126140

@@ -241,12 +255,14 @@ void updateFromParent(BootstrapBrand bootstrapBrand,
241255
}
242256

243257
@Override public void displayBadgeDrawable() {
244-
setCompoundDrawablesWithIntrinsicBounds(
245-
null,
246-
null,
247-
this.bootstrapBadge.getBadgeDrawable(),
248-
null);
249-
setCompoundDrawablePadding(DimenUtils.dpToPixels(4));
258+
if (bootstrapBadge != null) {
259+
setCompoundDrawablesWithIntrinsicBounds(
260+
null,
261+
null,
262+
this.bootstrapBadge.getBadgeDrawable(),
263+
null);
264+
setCompoundDrawablePadding(DimenUtils.dpToPixels(4));
265+
}
250266
}
251267

252268
/*
@@ -294,16 +310,21 @@ public void setChecked(boolean checked) {
294310
displayBadgeDrawable();
295311
}
296312

297-
@Override public void setBadgeCount(int badgeCount) {
298-
if (bootstrapBadge != null && badgeCount >= 0) {
299-
this.bootstrapBadge.setBadgeCount(badgeCount);
313+
@Nullable
314+
@Override
315+
public String getBadgeText() {
316+
return bootstrapBadge != null ? bootstrapBadge.getBadgeText() : null;
317+
}
318+
319+
@Override
320+
public void setBadgeText(@Nullable String badgeText) {
321+
if (bootstrapBadge != null) {
322+
this.badgeText = badgeText;
323+
this.bootstrapBadge.setBadgeText(this.badgeText);
300324
displayBadgeDrawable();
301325
}
302326
}
303327

304-
@Override public int getBadgeCount() {
305-
return bootstrapBadge != null ? bootstrapBadge.getBadgeCount() : 0;
306-
}
307328
@Override public float getBootstrapSize() {
308329
return bootstrapSize;
309330
}

AndroidBootstrap/src/main/java/com/beardedhen/androidbootstrap/BootstrapDrawableFactory.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,10 @@ static Drawable bootstrapAlert(Context context,
186186

187187
int strokeWidth = context.getResources().getDimensionPixelSize(R.dimen.bootstrap_alert_stroke_width);
188188

189-
disabledGd.setColor(ColorUtils.increaseOpacity(context, bootstrapBrand.getColor(), 40));
189+
disabledGd.setColor(ColorUtils.increaseOpacityFromInt(context, bootstrapBrand.getColor(),
190+
40));
190191
disabledGd.setCornerRadius(6);
191-
disabledGd.setStroke(strokeWidth, ColorUtils.increaseOpacity(context, bootstrapBrand.getColor(), 70));
192+
disabledGd.setStroke(strokeWidth, ColorUtils.increaseOpacityFromInt(context, bootstrapBrand.getColor(), 70));
192193
return disabledGd;
193194
}
194195

@@ -265,16 +266,16 @@ private static StateListDrawable setupStateDrawable(ViewGroupPosition position,
265266
return stateListDrawable;
266267
}
267268

268-
private static void setupDrawableCorners(ViewGroupPosition position, boolean rounded, int cornerRadius, GradientDrawable defaultGd, GradientDrawable activeGd, GradientDrawable disabledGd) {
269+
private static void setupDrawableCorners(ViewGroupPosition position, boolean rounded, int r,
270+
GradientDrawable defaultGd, GradientDrawable activeGd, GradientDrawable disabledGd) {
269271
if (rounded) {
270272
if (position == ViewGroupPosition.SOLO) {
271-
defaultGd.setCornerRadius(cornerRadius);
272-
activeGd.setCornerRadius(cornerRadius);
273-
disabledGd.setCornerRadius(cornerRadius);
273+
defaultGd.setCornerRadius(r);
274+
activeGd.setCornerRadius(r);
275+
disabledGd.setCornerRadius(r);
274276
}
275277
else {
276278
float[] radii; // X/Y pairs for top-left, top-right, bottom-right, bottom-left.
277-
float r = cornerRadius;
278279

279280
switch (position) {
280281
case MIDDLE_HORI:
@@ -434,8 +435,13 @@ private static Drawable createCloseCrossIcon(Context context, int width, int hei
434435
return new BitmapDrawable(context.getResources(), canvasBitmap);
435436
}
436437

437-
public static Drawable createBadgeDrawable(Context context, BootstrapBrand brand, int width, int height, int badgeCount, boolean allowZeroValue, boolean insideAnObject) {
438-
if (allowZeroValue || badgeCount > 0) {
438+
public static Drawable createBadgeDrawable(Context context, BootstrapBrand brand, int width,
439+
int height, String badgeText, boolean insideAnObject) {
440+
441+
if (badgeText == null) {
442+
return null;
443+
}
444+
else {
439445
Paint badgePaint = new Paint();
440446
Rect canvasBounds = new Rect();
441447
TextPaint badgeTextPaint = new TextPaint();
@@ -453,7 +459,7 @@ public static Drawable createBadgeDrawable(Context context, BootstrapBrand brand
453459
badgeTextPaint.setColor(brand.defaultTextColor(context));
454460
}
455461

456-
int rectLength = (int) badgeTextPaint.measureText(String.valueOf(badgeCount).substring(0, String.valueOf(badgeCount).length() - 1));
462+
int rectLength = (int) badgeTextPaint.measureText(badgeText);
457463

458464
Bitmap canvasBitmap = Bitmap.createBitmap(width + rectLength, height, Bitmap.Config.ARGB_8888);
459465
Canvas canvas = new Canvas(canvasBitmap);
@@ -473,13 +479,12 @@ public static Drawable createBadgeDrawable(Context context, BootstrapBrand brand
473479
canvas.drawCircle(firstCircleDx, circleDy, circleRadius, badgePaint);
474480
canvas.drawRect(rect, badgePaint);
475481
canvas.drawCircle(secondCircleDx, circleDy, circleRadius, badgePaint);
476-
canvas.drawText(String.valueOf(badgeCount), canvasBounds.width() / 2, canvasBounds.height() / 2 - ((badgeTextPaint.descent() + badgeTextPaint.ascent()) / 2), badgeTextPaint);
482+
canvas.drawText(badgeText, canvasBounds.width() / 2, canvasBounds.height() / 2 - ((badgeTextPaint.descent() +
483+
badgeTextPaint.ascent()) / 2),
484+
badgeTextPaint);
477485

478486
return new BitmapDrawable(context.getResources(), canvasBitmap);
479487
}
480-
else {
481-
return null;
482-
}
483488
}
484489

485490
static ColorStateList bootstrapDropDownViewText(Context context) {

AndroidBootstrap/src/main/java/com/beardedhen/androidbootstrap/BootstrapDropDown.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.beardedhen.androidbootstrap.api.view.RoundableView;
3030
import com.beardedhen.androidbootstrap.utils.ColorUtils;
3131
import com.beardedhen.androidbootstrap.utils.DimenUtils;
32+
import com.beardedhen.androidbootstrap.utils.DrawableUtils;
3233
import com.beardedhen.androidbootstrap.utils.ViewUtils;
3334

3435
import java.io.Serializable;
@@ -131,7 +132,8 @@ private void createDropDown() {
131132
dropdownWindow = new PopupWindow();
132133
dropdownWindow.setFocusable(true);
133134
dropdownWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
134-
dropdownWindow.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.dialog_holo_light_frame));
135+
dropdownWindow.setBackgroundDrawable(DrawableUtils.resolveDrawable(android.R.drawable
136+
.dialog_holo_light_frame, getContext()));
135137
dropdownWindow.setContentView(dropdownView);
136138
dropdownWindow.setOnDismissListener(this);
137139
dropdownWindow.setAnimationStyle(android.R.style.Animation_Activity);

AndroidBootstrap/src/main/java/com/beardedhen/androidbootstrap/api/view/BadgeContainerView.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Classes which implement this interface can be used as BootstrapBadge container.
77
* For example, a Button can be set up with BootstrapBadge.
88
*/
9-
public interface BadgeContainerView {
9+
public interface BadgeContainerView extends BootstrapBadgeView {
1010

1111
String KEY = "com.beardedhen.androidbootstrap.api.view.BadgeContainerView";
1212

@@ -17,27 +17,13 @@ public interface BadgeContainerView {
1717
*/
1818
void setBadge(BootstrapBadge badge);
1919

20-
/**
21-
* Sets badge count to be displayed to user.
22-
*
23-
* @param count badge count
24-
*/
25-
void setBadgeCount(int count);
26-
2720
/**
2821
* Retrieves BootstrapBadge object from container view.
2922
*
3023
* @return BootstrapBadge if exist or null if not
3124
*/
3225
BootstrapBadge getBootstrapBadge();
3326

34-
/**
35-
* Retrieves current badge count.
36-
*
37-
* @return badge count
38-
*/
39-
int getBadgeCount();
40-
4127
/**
4228
* Method where badge display logic must be implemented
4329
*
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.beardedhen.androidbootstrap.api.view;
2+
3+
import android.support.annotation.Nullable;
4+
5+
public interface BootstrapBadgeView {
6+
7+
/**
8+
* Retrieves the currently displayed badge text
9+
*
10+
* @return the badge text
11+
*/
12+
@Nullable
13+
String getBadgeText();
14+
15+
/**
16+
* Updates the badge to display a text string
17+
*
18+
* @param badgeText the badge text
19+
*/
20+
void setBadgeText(@Nullable String badgeText);
21+
22+
}

AndroidBootstrap/src/main/java/com/beardedhen/androidbootstrap/utils/ColorUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ public class ColorUtils {
7171
*/
7272
@ColorInt public static int increaseOpacity(Context context, @ColorRes int res, int alpha) {
7373
int c = resolveColor(res, context);
74+
return increaseOpacityFromInt(context, resolveColor(res, context), alpha);
75+
}
76+
77+
@ColorInt public static int increaseOpacityFromInt(Context context, @ColorInt int c, int
78+
alpha) {
7479
return Color.argb(alpha, Color.red(c), Color.green(c), Color.blue(c));
7580
}
7681

0 commit comments

Comments
 (0)