Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add badge text color option #845

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class BottomBarTest {
private static final int ACTIVE_TAB_COLOR = Color.parseColor("#222222");
private static final int BACKGROUND_COLOR = Color.parseColor("#333333");
private static final int BADGE_BACKGROUND_COLOR = Color.parseColor("#444444");
private static final int BADGE_TEXT_COLOR = Color.parseColor("#555555");
private static final boolean DEFAULT_BADGE_HIDES_WHEN_SELECTED_VALUE = true;
private static final int TITLE_TEXT_APPEARANCE = com.roughike.bottombar.test.R.style.dummy_text_appearance;
private static final Typeface TYPEFACE = Typeface.DEFAULT_BOLD;
Expand All @@ -56,6 +57,7 @@ public class BottomBarTest {
.activeTabColor(ACTIVE_TAB_COLOR)
.barColorWhenSelected(BACKGROUND_COLOR)
.badgeBackgroundColor(BADGE_BACKGROUND_COLOR)
.badgeTextColor(BADGE_TEXT_COLOR)
.hideBadgeWhenSelected(DEFAULT_BADGE_HIDES_WHEN_SELECTED_VALUE)
.titleTextAppearance(TITLE_TEXT_APPEARANCE)
.titleTypeFace(TYPEFACE)
Expand Down Expand Up @@ -115,6 +117,7 @@ public void setItemsWithCustomConfig_OverridesPreviousValues() {
assertEquals(ACTIVE_TAB_COLOR, first.getActiveColor());
assertEquals(BACKGROUND_COLOR, first.getBarColorWhenSelected());
assertEquals(BADGE_BACKGROUND_COLOR, first.getBadgeBackgroundColor());
assertEquals(BADGE_TEXT_COLOR, first.getBadgeTextColor());
assertEquals(DEFAULT_BADGE_HIDES_WHEN_SELECTED_VALUE, first.getBadgeHidesWhenActive());
assertEquals(TITLE_TEXT_APPEARANCE, first.getTitleTextAppearance());
assertEquals(TYPEFACE, first.getTitleTypeFace());
Expand Down Expand Up @@ -384,6 +387,7 @@ public void setInactiveTabAlpha_LeavesOtherValuesIntact() {
assertEquals(ACTIVE_TAB_COLOR, inActiveTab.getActiveColor());
assertEquals(BACKGROUND_COLOR, inActiveTab.getBarColorWhenSelected());
assertEquals(BADGE_BACKGROUND_COLOR, inActiveTab.getBadgeBackgroundColor());
assertEquals(BADGE_TEXT_COLOR, inActiveTab.getBadgeTextColor());
assertEquals(TITLE_TEXT_APPEARANCE, inActiveTab.getTitleTextAppearance());
assertEquals(TYPEFACE, inActiveTab.getTitleTypeFace());
}
Expand Down Expand Up @@ -417,6 +421,7 @@ public void setActiveTabAlpha_LeavesOtherValuesIntact() {
assertEquals(ACTIVE_TAB_COLOR, activeTab.getActiveColor());
assertEquals(BACKGROUND_COLOR, activeTab.getBarColorWhenSelected());
assertEquals(BADGE_BACKGROUND_COLOR, activeTab.getBadgeBackgroundColor());
assertEquals(BADGE_TEXT_COLOR, activeTab.getBadgeTextColor());
assertEquals(TITLE_TEXT_APPEARANCE, activeTab.getTitleTextAppearance());
assertEquals(TYPEFACE, activeTab.getTitleTypeFace());
}
Expand Down Expand Up @@ -455,6 +460,7 @@ public void setInactiveColor_LeavesOtherValuesIntact() {
assertEquals(ACTIVE_TAB_COLOR, inActiveTab.getActiveColor());
assertEquals(BACKGROUND_COLOR, inActiveTab.getBarColorWhenSelected());
assertEquals(BADGE_BACKGROUND_COLOR, inActiveTab.getBadgeBackgroundColor());
assertEquals(BADGE_TEXT_COLOR, inActiveTab.getBadgeTextColor());
assertEquals(TITLE_TEXT_APPEARANCE, inActiveTab.getTitleTextAppearance());
assertEquals(TYPEFACE, inActiveTab.getTitleTypeFace());
}
Expand Down Expand Up @@ -491,6 +497,7 @@ public void setActiveColor_LeavesOtherValuesIntact() {
assertEquals(Color.BLUE, inActiveTab.getActiveColor());
assertEquals(BACKGROUND_COLOR, inActiveTab.getBarColorWhenSelected());
assertEquals(BADGE_BACKGROUND_COLOR, inActiveTab.getBadgeBackgroundColor());
assertEquals(BADGE_TEXT_COLOR, inActiveTab.getBadgeTextColor());
assertEquals(TITLE_TEXT_APPEARANCE, inActiveTab.getTitleTextAppearance());
assertEquals(TYPEFACE, inActiveTab.getTitleTypeFace());
}
Expand Down Expand Up @@ -526,6 +533,37 @@ public void setBadgeBackgroundColor_LeavesOtherValuesIntact() {
assertEquals(TYPEFACE, inActiveTab.getTitleTypeFace());
}

@Test
@UiThreadTest
public void setBadgeTextColor_UpdatesColor() {
BottomBarTab inActiveTab = bottomBar.getTabAtPosition(1);
inActiveTab.setBadgeCount(3);

int previousBadgeTextColor = inActiveTab.getBadgeTextColor();
int testColor = Color.GREEN;
assertNotEquals(testColor, previousBadgeTextColor);

bottomBar.setBadgeTextColor(testColor);
assertEquals(testColor, inActiveTab.getBadgeTextColor());
}

@Test
public void setBadgeTextColor_LeavesOtherValuesIntact() {
bottomBar.setBadgeTextColor(Color.BLUE);

BottomBarTab inActiveTab = bottomBar.getTabAtPosition(1);
assertNotEquals(inActiveTab, bottomBar.getCurrentTab());

assertEquals(INACTIVE_TAB_ALPHA, inActiveTab.getInActiveAlpha(), 0);
assertEquals(ACTIVE_TAB_ALPHA, inActiveTab.getActiveAlpha(), 0);
assertEquals(INACTIVE_TAB_COLOR, inActiveTab.getInActiveColor());
assertEquals(ACTIVE_TAB_COLOR, inActiveTab.getActiveColor());
assertEquals(BACKGROUND_COLOR, inActiveTab.getBarColorWhenSelected());
assertEquals(Color.BLUE, inActiveTab.getBadgeTextColor());
assertEquals(TITLE_TEXT_APPEARANCE, inActiveTab.getTitleTextAppearance());
assertEquals(TYPEFACE, inActiveTab.getTitleTypeFace());
}

@Test
@UiThreadTest
public void setBadgeHidesWhenSelected_UpdatesBadgeHidesWhenSelected() {
Expand All @@ -550,6 +588,7 @@ public void setBadgeHidesWhenSelected_LeavesOtherValuesIntact() {
assertEquals(ACTIVE_TAB_COLOR, tab.getActiveColor());
assertEquals(BACKGROUND_COLOR, tab.getBarColorWhenSelected());
assertEquals(BADGE_BACKGROUND_COLOR, tab.getBadgeBackgroundColor());
assertEquals(BADGE_TEXT_COLOR, tab.getBadgeTextColor());
assertTrue(tab.getBadgeHidesWhenActive());
assertEquals(TITLE_TEXT_APPEARANCE, tab.getTitleTextAppearance());
assertEquals(TYPEFACE, tab.getTitleTypeFace());
Expand Down Expand Up @@ -582,6 +621,7 @@ public void setTitleTextAppearance_LeavesOtherValuesIntact() {
assertEquals(ACTIVE_TAB_COLOR, inActiveTab.getActiveColor());
assertEquals(BACKGROUND_COLOR, inActiveTab.getBarColorWhenSelected());
assertEquals(BADGE_BACKGROUND_COLOR, inActiveTab.getBadgeBackgroundColor());
assertEquals(BADGE_TEXT_COLOR, inActiveTab.getBadgeTextColor());
assertEquals(-666, inActiveTab.getTitleTextAppearance());
assertEquals(TYPEFACE, inActiveTab.getTitleTypeFace());
}
Expand Down Expand Up @@ -614,6 +654,7 @@ public void setTitleTypeface_LeavesOtherValuesIntact() {
assertEquals(ACTIVE_TAB_COLOR, inActiveTab.getActiveColor());
assertEquals(BACKGROUND_COLOR, inActiveTab.getBarColorWhenSelected());
assertEquals(BADGE_BACKGROUND_COLOR, inActiveTab.getBadgeBackgroundColor());
assertEquals(BADGE_TEXT_COLOR, inActiveTab.getBadgeTextColor());
assertEquals(TITLE_TEXT_APPEARANCE, inActiveTab.getTitleTextAppearance());
assertEquals(Typeface.DEFAULT, inActiveTab.getTitleTypeFace());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ public void badgeBackgroundColorAsExpected() {
assertEquals(Color.parseColor("#00F0F0"), tabs.get(4).getBadgeBackgroundColor());
}

@Test
public void badgeTextColorAsExpected() {
assertEquals(Color.parseColor("#FF0000"), tabs.get(0).getBadgeTextColor());
assertEquals(Color.parseColor("#00FF00"), tabs.get(1).getBadgeTextColor());
assertEquals(Color.parseColor("#F00000"), tabs.get(2).getBadgeTextColor());
assertEquals(Color.parseColor("#00F000"), tabs.get(3).getBadgeTextColor());
assertEquals(Color.parseColor("#00F0F0"), tabs.get(4).getBadgeTextColor());
}

@Test
public void correctBadgeHidingPolicies() {
assertFalse(tabs.get(0).getBadgeHidesWhenActive());
Expand Down
10 changes: 5 additions & 5 deletions bottom-bar/src/androidTest/res/xml/dummy_tabs_five.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<tabs>
<tab inActiveColor="#00FF00" activeColor="#FF0000" id="@+id/tab_recents" title="Recents" icon="@drawable/empty_icon" barColorWhenSelected="#FF0000" badgeBackgroundColor="#FF0000" badgeHidesWhenActive="false" />
<tab inActiveColor="#0000FF" activeColor="@color/test_random_color" id="@+id/tab_favorites" title="@string/favorites" icon="@drawable/empty_icon" barColorWhenSelected="#00FF00" badgeBackgroundColor="#00FF00" />
<tab inActiveColor="#FF0000" activeColor="#0000FF" id="@+id/tab_nearby" title="Nearby" icon="@drawable/empty_icon" barColorWhenSelected="#F00000" badgeBackgroundColor="#F00000" badgeHidesWhenActive="false" iconOnly="true" />
<tab inActiveColor="#F0F000" activeColor="@color/test_random_color_two" id="@+id/tab_friends" title="@string/friends" icon="@drawable/empty_icon" barColorWhenSelected="#00F000" badgeBackgroundColor="#00F000" badgeHidesWhenActive="true" />
<tab inActiveColor="#F00F00" activeColor="#F00F00" id="@+id/tab_food" title="Food" icon="@drawable/empty_icon" barColorWhenSelected="#00F0F0" badgeBackgroundColor="#00F0F0" iconOnly="true" />
<tab inActiveColor="#00FF00" activeColor="#FF0000" id="@+id/tab_recents" title="Recents" icon="@drawable/empty_icon" barColorWhenSelected="#FF0000" badgeBackgroundColor="#FF0000" badgeHidesWhenActive="false" badgeTextColor="#FF0000" />
<tab inActiveColor="#0000FF" activeColor="@color/test_random_color" id="@+id/tab_favorites" title="@string/favorites" icon="@drawable/empty_icon" barColorWhenSelected="#00FF00" badgeBackgroundColor="#00FF00" badgeTextColor="#00FF00" />
<tab inActiveColor="#FF0000" activeColor="#0000FF" id="@+id/tab_nearby" title="Nearby" icon="@drawable/empty_icon" barColorWhenSelected="#F00000" badgeBackgroundColor="#F00000" badgeHidesWhenActive="false" iconOnly="true" badgeTextColor="#F00000" />
<tab inActiveColor="#F0F000" activeColor="@color/test_random_color_two" id="@+id/tab_friends" title="@string/friends" icon="@drawable/empty_icon" barColorWhenSelected="#00F000" badgeBackgroundColor="#00F000" badgeHidesWhenActive="true" badgeTextColor="#00F000" />
<tab inActiveColor="#F00F00" activeColor="#F00F00" id="@+id/tab_food" title="Food" icon="@drawable/empty_icon" barColorWhenSelected="#00F0F0" badgeBackgroundColor="#00F0F0" iconOnly="true" badgeTextColor="#00F0F0" />
</tabs>
14 changes: 14 additions & 0 deletions bottom-bar/src/main/java/com/roughike/bottombar/BottomBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class BottomBar extends LinearLayout implements View.OnClickListener, Vie
private int inActiveTabColor;
private int activeTabColor;
private int badgeBackgroundColor;
private int badgeTextColor;
private boolean hideBadgeWhenActive;
private boolean longPressHintsEnabled;
private int titleTextAppearance;
Expand Down Expand Up @@ -208,6 +209,7 @@ private void populateAttributes(Context context, AttributeSet attrs, int defStyl
inActiveTabColor = ta.getColor(R.styleable.BottomBar_bb_inActiveTabColor, defaultInActiveColor);
activeTabColor = ta.getColor(R.styleable.BottomBar_bb_activeTabColor, defaultActiveColor);
badgeBackgroundColor = ta.getColor(R.styleable.BottomBar_bb_badgeBackgroundColor, Color.RED);
badgeTextColor = ta.getColor(R.styleable.BottomBar_bb_badgeTextColor, Color.WHITE);
hideBadgeWhenActive = ta.getBoolean(R.styleable.BottomBar_bb_badgesHideWhenActive, true);
titleTextAppearance = ta.getResourceId(R.styleable.BottomBar_bb_titleTextAppearance, 0);
titleTypeFace = getTypeFaceFromAsset(ta.getString(R.styleable.BottomBar_bb_titleTypeFace));
Expand Down Expand Up @@ -318,6 +320,7 @@ private BottomBarTab.Config getTabConfig() {
.activeTabColor(activeTabColor)
.barColorWhenSelected(defaultBackgroundColor)
.badgeBackgroundColor(badgeBackgroundColor)
.badgeTextColor(badgeTextColor)
.hideBadgeWhenSelected(hideBadgeWhenActive)
.titleTextAppearance(titleTextAppearance)
.titleTypeFace(titleTypeFace)
Expand Down Expand Up @@ -697,6 +700,17 @@ public void update(BottomBarTab tab) {
});
}

public void setBadgeTextColor(@ColorInt int color) {
badgeTextColor = color;

batchPropertyApplier.applyToAllTabs(new BatchTabPropertyApplier.TabPropertyUpdater() {
@Override
public void update(BottomBarTab tab) {
tab.setBadgeTextColor(badgeTextColor);
}
});
}

/**
* Controls whether the badge (if any) for active tabs
* should be hidden or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ void setColoredCircleBackground(int circleColor) {
setBackgroundCompat(backgroundCircle);
}

void setBadgeTextColor(int badgeTextColor) {
setTextColor(badgeTextColor);
}

private void wrapTabAndBadgeInSameContainer(final BottomBarTab tab) {
ViewGroup tabContainer = (ViewGroup) tab.getParent();
tabContainer.removeView(tab);
Expand Down
23 changes: 23 additions & 0 deletions bottom-bar/src/main/java/com/roughike/bottombar/BottomBarTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class BottomBarTab extends LinearLayout {
private int activeColor;
private int barColorWhenSelected;
private int badgeBackgroundColor;
private int badgeTextColor;
private boolean badgeHidesWhenActive;
private AppCompatImageView iconView;
private TextView titleView;
Expand All @@ -84,6 +85,7 @@ void setConfig(@NonNull Config config) {
setActiveColor(config.activeTabColor);
setBarColorWhenSelected(config.barColorWhenSelected);
setBadgeBackgroundColor(config.badgeBackgroundColor);
setBadgeTextColor(config.badgeTextColor);
setBadgeHidesWhenActive(config.badgeHidesWhenSelected);
setTitleTextAppearance(config.titleTextAppearance);
setTitleTypeface(config.titleTypeFace);
Expand Down Expand Up @@ -280,6 +282,18 @@ public void setBadgeBackgroundColor(int badgeBackgroundColor) {
}
}

public int getBadgeTextColor() {
return badgeTextColor;
}

public void setBadgeTextColor(int badgeTextColor) {
this.badgeTextColor = badgeTextColor;

if (badge != null) {
badge.setBadgeTextColor(badgeBackgroundColor);
}
}

public boolean getBadgeHidesWhenActive() {
return badgeHidesWhenActive;
}
Expand Down Expand Up @@ -329,6 +343,7 @@ public void setBadgeCount(int count) {
if (badge == null) {
badge = new BottomBarBadge(getContext());
badge.attachToTab(this, badgeBackgroundColor);
badge.setTextColor(badgeTextColor);
}

badge.setCount(count);
Expand Down Expand Up @@ -647,6 +662,7 @@ public static class Config {
private final int activeTabColor;
private final int barColorWhenSelected;
private final int badgeBackgroundColor;
private final int badgeTextColor;
private final int titleTextAppearance;
private final Typeface titleTypeFace;
private boolean badgeHidesWhenSelected = true;
Expand All @@ -658,6 +674,7 @@ private Config(Builder builder) {
this.activeTabColor = builder.activeTabColor;
this.barColorWhenSelected = builder.barColorWhenSelected;
this.badgeBackgroundColor = builder.badgeBackgroundColor;
this.badgeTextColor = builder.badgeTextColor;
this.badgeHidesWhenSelected = builder.hidesBadgeWhenSelected;
this.titleTextAppearance = builder.titleTextAppearance;
this.titleTypeFace = builder.titleTypeFace;
Expand All @@ -670,6 +687,7 @@ public static class Builder {
private int activeTabColor;
private int barColorWhenSelected;
private int badgeBackgroundColor;
private int badgeTextColor;
private boolean hidesBadgeWhenSelected = true;
private int titleTextAppearance;
private Typeface titleTypeFace;
Expand Down Expand Up @@ -704,6 +722,11 @@ public Builder badgeBackgroundColor(@ColorInt int color) {
return this;
}

public Builder badgeTextColor(@ColorInt int color) {
this.badgeTextColor = color;
return this;
}

public Builder hideBadgeWhenSelected(boolean hide) {
this.hidesBadgeWhenSelected = hide;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import static com.roughike.bottombar.TabParser.TabAttribute.ACTIVE_COLOR;
import static com.roughike.bottombar.TabParser.TabAttribute.BADGE_BACKGROUND_COLOR;
import static com.roughike.bottombar.TabParser.TabAttribute.BADGE_TEXT_COLOR;
import static com.roughike.bottombar.TabParser.TabAttribute.BADGE_HIDES_WHEN_ACTIVE;
import static com.roughike.bottombar.TabParser.TabAttribute.BAR_COLOR_WHEN_SELECTED;
import static com.roughike.bottombar.TabParser.TabAttribute.ICON;
Expand Down Expand Up @@ -134,6 +135,11 @@ private BottomBarTab parseNewTab(@NonNull XmlResourceParser parser, @IntRange(fr
if (badgeBackgroundColor == COLOR_NOT_SET) continue;
workingTab.setBadgeBackgroundColor(badgeBackgroundColor);
break;
case BADGE_TEXT_COLOR:
int badgeTextColor = getColorValue(parser, i);
if (badgeTextColor == COLOR_NOT_SET) continue;
workingTab.setBadgeTextColor(badgeTextColor);
break;
case BADGE_HIDES_WHEN_ACTIVE:
boolean badgeHidesWhenActive = parser.getAttributeBooleanValue(i, true);
workingTab.setBadgeHidesWhenActive(badgeHidesWhenActive);
Expand Down Expand Up @@ -188,6 +194,7 @@ private int getColorValue(@NonNull XmlResourceParser parser, @IntRange(from = 0)
ACTIVE_COLOR,
BAR_COLOR_WHEN_SELECTED,
BADGE_BACKGROUND_COLOR,
BADGE_TEXT_COLOR,
BADGE_HIDES_WHEN_ACTIVE,
IS_TITLELESS
})
Expand All @@ -199,6 +206,7 @@ private int getColorValue(@NonNull XmlResourceParser parser, @IntRange(from = 0)
String ACTIVE_COLOR = "activeColor";
String BAR_COLOR_WHEN_SELECTED = "barColorWhenSelected";
String BADGE_BACKGROUND_COLOR = "badgeBackgroundColor";
String BADGE_TEXT_COLOR = "badgeTextColor";
String BADGE_HIDES_WHEN_ACTIVE = "badgeHidesWhenActive";
String IS_TITLELESS = "iconOnly";
}
Expand Down
1 change: 1 addition & 0 deletions bottom-bar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<attr name="bb_inActiveTabColor" format="color" />
<attr name="bb_activeTabColor" format="color" />
<attr name="bb_badgeBackgroundColor" format="color" />
<attr name="bb_badgeTextColor" format="color" />
<attr name="bb_badgesHideWhenActive" format="boolean" />
<attr name="bb_titleTextAppearance" format="reference" />
<attr name="bb_titleTypeFace" format="string" />
Expand Down