DEV Community

myougaTheAxo
myougaTheAxo

Posted on

Badge & Counter in Compose: Notification Badges with BadgedBox

Badge & Counter in Compose: Notification Badges with BadgedBox

Master notification badges in Jetpack Compose using BadgedBox, Badge, and custom animations.

BadgedBox Basics

BadgedBox wraps content and displays a badge (Circle or custom):

BadgedBox(
    badge = { Badge() }
) {
    Icon(Icons.Default.ShoppingCart, contentDescription = "Cart")
}
Enter fullscreen mode Exit fullscreen mode

Badge Styles

Number Badge

Badge(
    modifier = Modifier.offset(x = 8.dp, y = (-8).dp),
    containerColor = Color.Red,
    contentColor = Color.White
) {
    Text("3")
}
Enter fullscreen mode Exit fullscreen mode

Dot Badge

BadgedBox(badge = { Badge() }) {
    Icon(Icons.Default.Notifications, "Notifications")
}
Enter fullscreen mode Exit fullscreen mode

BottomNavigation with Badges

BottomAppBar {
    NavigationBarItem(
        icon = {
            BadgedBox(
                badge = {
                    Badge {
                        Text(
                            (notificationCount.coerceAtMost(99) + if (notificationCount > 99) "+" else "").toString(),
                            fontSize = 10.sp
                        )
                    }
                }
            ) {
                Icon(Icons.Default.Mail, "Messages")
            }
        },
        selected = selectedTab == 0,
        onClick = { selectedTab = 0 }
    )
}
Enter fullscreen mode Exit fullscreen mode

Custom Badge with Box + Offset

Create custom badges with absolute positioning:

Box(modifier = Modifier.size(48.dp)) {
    Icon(Icons.Default.ShoppingCart, "Cart", Modifier.align(Alignment.Center))
    Badge(
        modifier = Modifier
            .align(Alignment.TopEnd)
            .offset(x = 6.dp, y = (-6).dp),
        containerColor = Color.Red
    ) {
        Text("5", fontSize = 10.sp, color = Color.White)
    }
}
Enter fullscreen mode Exit fullscreen mode

Animated Badge with Spring Bounce

val scale by animateFloatAsState(
    targetValue = if (isNew) 1.2f else 1f,
    animationSpec = spring(dampingRatio = 0.5f, stiffness = Spring.StiffnessHigh),
    label = "badge_scale"
)

BadgedBox(
    badge = {
        Badge(
            modifier = Modifier.scale(scale),
            containerColor = Color.Red
        ) {
            Text("!")
        }
    }
) {
    Icon(Icons.Default.Notifications, "Alert")
}
Enter fullscreen mode Exit fullscreen mode

Best Practices

  • Use offset() to fine-tune badge positioning
  • Animate badge appearance for notifications
  • Keep badges concise (show "99+" for large counts)
  • Test badge visibility across screen sizes

Master Compose UI patterns and build professional Android apps.

8 Android App Templates → https://myougatheax.gumroad.com

Top comments (0)