Skip to content
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 @@ -40,7 +40,14 @@ public void onTabReSelected(@IdRes int tabId) {
}
});

BottomBarTab favorites = bottomBar.getTabWithId(R.id.tab_favorites);
favorites.setBadgeCount(1200);

BottomBarTab nearby = bottomBar.getTabWithId(R.id.tab_nearby);
nearby.setBadgeCount(5);

BottomBarTab friends = bottomBar.getTabWithId(R.id.tab_friends);
friends.setBadgeCount(1200, true);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,15 @@ public void badgeRemovedProperly() {
assertNull(nearby.badge);
assertEquals(bottomBar.findViewById(R.id.bb_bottom_bar_item_container), nearby.getOuterView());
}

@Test
@UiThreadTest
public void badgeFormatTest() {
nearby.setBadgeCount(1200, true);
assertNotNull(nearby.badge);
assertEquals(nearby.badge.getText(), "1K");

nearby.setBadgeCount(0, true);
assertNull(nearby.badge);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ class BottomBarBadge extends TextView {
*
* @param count the value this Badge should show.
*/
void setCount(int count) {
void setCount(int count, boolean formatBadge) {
this.count = count;
setText(String.valueOf(count));
if (formatBadge && count > 1000) {
setText(String.format(getResources().getString(R.string.badge_format), count / 1000));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R.string.badge_format can be defined as %1$dK, then you can use it without calling String.format() as follows:
getResources().getString(R.string.badge_format), count / 1000).

} else {
setText(String.valueOf(count));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ int getCurrentDisplayedTextAppearance() {
}

public void setBadgeCount(int count) {
setBadgeCount(count, false);
}

public void setBadgeCount(int count, boolean formatBadge) {
if (count <= 0) {
if (badge != null) {
badge.removeFromTab(this);
Expand All @@ -331,7 +335,7 @@ public void setBadgeCount(int count) {
badge.attachToTab(this, badgeBackgroundColor);
}

badge.setCount(count);
badge.setCount(count, formatBadge);

if (isActive && badgeHidesWhenActive) {
badge.hide();
Expand Down
1 change: 1 addition & 0 deletions bottom-bar/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">BottomBar</string>
<string name="badge_format">%dK</string>
</resources>