skip to Main Content

With Jetpack Compose version "1.0.1" I used Badge Box like This

BadgedBox(
    badgeContent = {
        Text(text = "5")
    },
    backgroundColor = Primary,
    modifier = Modifier
) {
      Icon(
          imageVector = Icons.Outlined.ShoppingCart,
          contentDescription = "shopping cart",
          tint = Color.Black
      )
}

But with Compose version "1.0.5" this code doesn’t work

What is the new way to do it?

2

Answers


  1. Using badge attribute of BadgedBox and Badge Composable.

    Example,

    BadgedBox(
        badge = {
            Badge {
                Text(text = "5")
            }
        },
        // Other attributes remain same
    ) {
          // As it is
    }
    
    Login or Signup to reply.
  2. With compose M2 (starting from 1.1.x) and M3 you can use:

    BadgedBox(
        badge = { Badge { Text("5") } },
        modifier = Modifier.background(Red)) {
            Icon(
                Icons.Filled.Favorite,
                contentDescription = "Favorite"
            )
    }
    

    Old answer:
    Instead with compose M2 1.0.x you can use:

    BadgeBox(
        badgeContent = { Text("5") },
        backgroundColor = Red,) {
           Icon(
               Icons.Filled.Favorite,
               contentDescription = "Favorite"
           )
    }
    

    With 1.1.x the BadgeBox was renamed to BadgedBox and some parameters were removed (check this commit).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search