DEV Community

myougaTheAxo
myougaTheAxo

Posted on

BadgedBox: Notification Badges and Count Indicators

BadgedBox: Notification Badges and Count Indicators

BadgedBox displays notification indicators on UI elements, essential for showing unread counts or status.

Simple Badge

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

Badge with Count

BadgedBox(
    badge = {
        Badge(
            containerColor = Color.Red,
            contentColor = Color.White
        ) {
            Text("5", fontSize = 10.sp)
        }
    }
) {
    Icon(Icons.Default.ShoppingCart, contentDescription = "Cart")
}
Enter fullscreen mode Exit fullscreen mode

Animated Badge

var unreadCount by remember { mutableStateOf(0) }

BadgedBox(
    badge = {
        if (unreadCount > 0) {
            Badge {
                Text(unreadCount.toString())
            }
        }
    }
) {
    Icon(Icons.Default.Notifications, contentDescription = "Notifications")
}
Enter fullscreen mode Exit fullscreen mode

Custom Badge Position

Box(
    modifier = Modifier.fillMaxSize()
) {
    Icon(Icons.Default.Favorite, contentDescription = "Like")
    Badge(
        modifier = Modifier.align(Alignment.TopEnd),
        containerColor = Color.Green
    )
}
Enter fullscreen mode Exit fullscreen mode

Badges are subtle but powerful UI elements that keep users informed about important updates without being intrusive.


8 Android app templates available on Gumroad

Top comments (0)