In This Jetpack compose tutorial we will cover about BadgeBox compose widget. Sometimes we may required to display notification,email,chat counts on the respected UI components. To display this type of count, compose given a widget called BadgeBox.
A BadgeBox is used to decorate content with a badge that can contain dynamic information, such as the presence of a new notification or a number of pending requests. Badges can be icon only or contain short text.
BadgeBox Properties:
badge - the badge to be displayed - typically a Badge
modifier - optional Modifier for this item
content - the anchor to which this badge will be positioned
@Composable |
In this sample we will create simple BadgeBox which will show the count when we tap on button click.
Let's get started
Step 1: Create Compose application in Android Studio, Pease refer how to create new Project in Android studio using Jetpack compose
Step 2: Create badge with Icon and Text
BadgedBox(badge ={Text("HERE our badge ", |
Step 3: Now add some functionality to set badge count
We are saving the badge count with create a varible with mutableState value
var favCounter = remember {mutableStateOf(1)}
Update this value on button click
Button( |
Step 4: Add value to badge by
BadgedBox(badge ={Text("${favCounter.value}", |
Step 5: Now run application
You will see below outout
Complete example code for How do I create badge in Compose application
package com.rrtutors.compose_tutorials import android.annotation.SuppressLint class MainActivity : ComponentActivity() { setContent { Spacer(modifier = Modifier.requiredWidth(12.dp)) BottomNavigation(backgroundColor = Color.White) {
}, }, }, }
@Preview(showBackground = true) |
Article Contributed By :
|
|
|
|
584 Views |