diff --git a/app/src/main/java/com/example/bottombar/sample/BadgeActivity.java b/app/src/main/java/com/example/bottombar/sample/BadgeActivity.java index dd6f41de..19c7719b 100644 --- a/app/src/main/java/com/example/bottombar/sample/BadgeActivity.java +++ b/app/src/main/java/com/example/bottombar/sample/BadgeActivity.java @@ -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); + } } diff --git a/bottom-bar/src/androidTest/java/com/roughike/bottombar/BadgeTest.java b/bottom-bar/src/androidTest/java/com/roughike/bottombar/BadgeTest.java index df62e3ec..00d426be 100644 --- a/bottom-bar/src/androidTest/java/com/roughike/bottombar/BadgeTest.java +++ b/bottom-bar/src/androidTest/java/com/roughike/bottombar/BadgeTest.java @@ -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); + } } diff --git a/bottom-bar/src/main/java/com/roughike/bottombar/BottomBarBadge.java b/bottom-bar/src/main/java/com/roughike/bottombar/BottomBarBadge.java index af5b8546..f65a34e5 100644 --- a/bottom-bar/src/main/java/com/roughike/bottombar/BottomBarBadge.java +++ b/bottom-bar/src/main/java/com/roughike/bottombar/BottomBarBadge.java @@ -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)); + } else { + setText(String.valueOf(count)); + } } /** diff --git a/bottom-bar/src/main/java/com/roughike/bottombar/BottomBarTab.java b/bottom-bar/src/main/java/com/roughike/bottombar/BottomBarTab.java index d33e1e30..fb330e63 100644 --- a/bottom-bar/src/main/java/com/roughike/bottombar/BottomBarTab.java +++ b/bottom-bar/src/main/java/com/roughike/bottombar/BottomBarTab.java @@ -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); @@ -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(); diff --git a/bottom-bar/src/main/res/values/strings.xml b/bottom-bar/src/main/res/values/strings.xml index 88057c07..cd9330ba 100644 --- a/bottom-bar/src/main/res/values/strings.xml +++ b/bottom-bar/src/main/res/values/strings.xml @@ -1,3 +1,4 @@ BottomBar + %dK