1# Badge 2 3The **Badge** component is a container that can be attached to another component for notification and reminder purposes. 4 5> **NOTE** 6> 7> This component is supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version. 8 9 10## Child Components 11 12This component supports only one child component. 13 14> **NOTE** 15> 16> Supported types of child components: built-in components and custom components, with support for ([if/else](../../../quick-start/arkts-rendering-control-ifelse.md), [ForEach](../../../quick-start/arkts-rendering-control-foreach.md), and [LazyForEach](../../../quick-start/arkts-rendering-control-lazyforeach.md)) rendering control. 17 18 19## APIs 20 21### Badge 22 23Badge(value: BadgeParamWithNumber) 24 25Creates a badge with the given numerical value. 26 27**Widget capability**: This API can be used in ArkTS widgets since API version 9. 28 29**Atomic service API**: This API can be used in atomic services since API version 11. 30 31**System capability**: SystemCapability.ArkUI.ArkUI.Full 32 33**Parameters** 34 35| Name| Type| Mandatory| Description| 36| -------- | -------- | -------- | -------- | 37| value | [BadgeParamWithNumber](#badgeparamwithnumber)| Yes| Options of the numeric badge.| 38 39### Badge 40 41Badge(value: BadgeParamWithString) 42 43Creates a badge with the given string. 44 45**Widget capability**: This API can be used in ArkTS widgets since API version 9. 46 47**Atomic service API**: This API can be used in atomic services since API version 11. 48 49**System capability**: SystemCapability.ArkUI.ArkUI.Full 50 51This component supports the scaling effect for visibility transition since API version 12. 52 53**Parameters** 54 55| Name| Type | Mandatory| Description | 56| ------ | ----------------------------------------------------- | ---- | -------------------- | 57| value | [BadgeParamWithString](#badgeparamwithstring) | Yes | Options of the string-type badge.| 58 59## BadgeParam 60 61Provides basic parameters for creating a badge. 62 63**Widget capability**: This API can be used in ArkTS widgets since API version 9. 64 65**Atomic service API**: This API can be used in atomic services since API version 11. 66 67**System capability**: SystemCapability.ArkUI.ArkUI.Full 68 69| Name| Type| Mandatory| Description| 70| -------- | -------- | -------- | -------- | 71| position | [BadgePosition](#badgeposition)\|[Position<sup>10+</sup>](ts-types.md#position) | No| Position to display the badge relative to the parent component.<br>Default value: **BadgePosition.RightTop**<br>**NOTE**<br> With the **Position** type, percentage values are not supported. If an invalid value is set, the default value **(0,0)**, which indicates the upper left corner of the component, will be used.<br>With the **BadgePosition** type, the position is mirrored based on the [Direction](ts-appendix-enums.md#direction) property.| 72| style | [BadgeStyle](#badgestyle) | Yes| Style of the badge, including the font color, font size, badge color, and badge size.| 73 74 75## BadgeParamWithNumber 76 77Inherits from [BadgeParam](#badgeparam) and has all attributes of **BadgeParam**. 78 79**Widget capability**: This API can be used in ArkTS widgets since API version 9. 80 81**Atomic service API**: This API can be used in atomic services since API version 11. 82 83**System capability**: SystemCapability.ArkUI.ArkUI.Full 84 85| Name| Type| Mandatory| Description| 86| -------- | -------- | -------- | -------- | 87| count | number | Yes| Number of notifications.<br>**NOTE**<br>If the value is less than or equal to 0 and less than the value of **maxCount**, no badge is displayed.<br>Value range: [-2147483648, 2147483647]<br>If the value is out of the range, it will be adjusted by adding or subtracting 4294967296 to bring it back within the range. If the value is not an integer, it is rounded off to the nearest integer. For example, 5.5 is rounded off to 5.| 88| maxCount | number | No| Maximum number of notifications. When the maximum number is reached, only **maxCount+** is displayed.<br>Default value: **99**<br>Value range: [-2147483648, 2147483647]<br>If the value is out of the range, it will be adjusted by adding or subtracting 4294967296 to bring it back within the range. If the value is not an integer, it is rounded off to the nearest integer. For example, 5.5 is rounded off to 5.| 89 90## BadgeParamWithString 91 92Inherits from [BadgeParam](#badgeparam) and has all attributes of **BadgeParam**. 93 94**Widget capability**: This API can be used in ArkTS widgets since API version 9. 95 96**Atomic service API**: This API can be used in atomic services since API version 11. 97 98**System capability**: SystemCapability.ArkUI.ArkUI.Full 99 100| Name| Type| Mandatory| Description| 101| -------- | -------- | -------- | -------- | 102| value | string | Yes| Prompt content.| 103 104## BadgePosition 105 106**Widget capability**: This API can be used in ArkTS widgets since API version 9. 107 108**Atomic service API**: This API can be used in atomic services since API version 11. 109 110**System capability**: SystemCapability.ArkUI.ArkUI.Full 111 112| Name| Value| Description| 113| -------- | -------- |-------- | 114| RightTop | 0 | The badge is displayed in the upper right corner of the parent component.| 115| Right | 1 | The badge is vertically centered on the right of the parent component.| 116| Left | 2 | The badge is vertically centered on the left of the parent component.| 117 118## BadgeStyle 119 120**Atomic service API**: This API can be used in atomic services since API version 11. 121 122**System capability**: SystemCapability.ArkUI.ArkUI.Full 123 124| Name | Type | Mandatory| Description | 125| ------------------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 126| color | [ResourceColor](ts-types.md#resourcecolor) | No | Font color.<br>Default value: **Color.White**<br>**Widget capability**: This API can be used in ArkTS widgets since API version 9.| 127| fontSize | number \| string | No | Font size. For the string type, only numeric string values with optional units, for example, **"10"** or **"10fp"**, are supported.<br>Default value: **10**<br>Unit: fp<br>The value must be greater than or equal to 0. If the value is less than 0, the default value is used.<br>**NOTE**<br>This parameter cannot be set in percentage.<br>**Widget capability**: This API can be used in ArkTS widgets since API version 9.| 128| badgeSize | number \| string | No | Badge size. For the string type, numeric string values with optional units, for example, **"16"** or **"16fp"**, are supported.<br>Default value: **16**<br>Unit: vp<br>The value must be greater than or equal to 0. If the value is less than 0, the default value is used.<br>**NOTE**<br>This parameter cannot be set in percentage. If it is set to an invalid value, the default value is used.<br>**Widget capability**: This API can be used in ArkTS widgets since API version 9.| 129| badgeColor | [ResourceColor](ts-types.md#resourcecolor) | No | Badge color.<br>Default value: **Color.Red**<br>**Widget capability**: This API can be used in ArkTS widgets since API version 9.| 130| fontWeight<sup>10+</sup> | number \|[FontWeight](ts-appendix-enums.md#fontweight) \| string | No | Font weight of the text. For the number type, the value ranges from 100 to 900, at an interval of 100. The default value is **400**. A larger value indicates a heavier font weight. For the string type, only strings that represent a number, for example, **400**, and the following enumerated values of **FontWeight** are supported: **bold**, **bolder**, **lighter**, **regular**, and **medium**.<br>Default value: **FontWeight.Normal**<br>**NOTE**<br>This parameter cannot be set in percentage.| 131| borderColor<sup>10+</sup> | [ResourceColor](ts-types.md#resourcecolor) | No | Border color of the background.<br>Default value: **Color.Red** | 132| borderWidth<sup>10+</sup> | [Length](ts-types.md#length) | No | Border width of the background.<br>Default value: **1**<br>Unit: vp<br>**NOTE**<br>This parameter cannot be set in percentage.| 133 134## Attributes 135 136The [universal attributes](ts-component-general-attributes.md) are supported. 137 138## Events 139 140The [universal events](ts-component-general-events.md) are supported. 141 142## Example 143 144### Example 1: Setting the Badge Content 145 146This example illustrates the varying visual effects of the **Badge** component when it receives empty values, character strings, and numerical values through the **value** and **count** properties. 147 148```ts 149// xxx.ets 150@Entry 151@Component 152struct BadgeExample { 153 @Builder 154 tabBuilder(index: number) { 155 Column() { 156 if (index === 2) { 157 Badge({ 158 value: '', 159 style: { badgeSize: 6, badgeColor: '#FA2A2D' } 160 }) { 161 Image('/common/public_icon_off.svg') 162 .width(24) 163 .height(24) 164 } 165 .width(24) 166 .height(24) 167 .margin({ bottom: 4 }) 168 } else { 169 Image('/common/public_icon_off.svg') 170 .width(24) 171 .height(24) 172 .margin({ bottom: 4 }) 173 } 174 Text('Tab') 175 .fontColor('#182431') 176 .fontSize(10) 177 .fontWeight(500) 178 .lineHeight(14) 179 }.width('100%').height('100%').justifyContent(FlexAlign.Center) 180 } 181 182 @Builder 183 itemBuilder(value: string) { 184 Row() { 185 Image('common/public_icon.svg').width(32).height(32).opacity(0.6) 186 Text(value) 187 .width(177) 188 .height(21) 189 .margin({ left: 15, right: 76 }) 190 .textAlign(TextAlign.Start) 191 .fontColor('#182431') 192 .fontWeight(500) 193 .fontSize(16) 194 .opacity(0.9) 195 Image('common/public_icon_arrow_right.svg').width(12).height(24).opacity(0.6) 196 }.width('100%').padding({ left: 12, right: 12 }).height(56) 197 } 198 199 build() { 200 Column() { 201 // Badge of the red dot type 202 Text('dotsBadge').fontSize(18).fontColor('#182431').fontWeight(500).margin(24) 203 Tabs() { 204 TabContent() 205 .tabBar(this.tabBuilder(0)) 206 TabContent() 207 .tabBar(this.tabBuilder(1)) 208 TabContent() 209 .tabBar(this.tabBuilder(2)) 210 TabContent() 211 .tabBar(this.tabBuilder(3)) 212 } 213 .width(360) 214 .height(56) 215 .backgroundColor('#F1F3F5') 216 217 // Create a badge with the given string. 218 Column() { 219 Text('stringBadge').fontSize(18).fontColor('#182431').fontWeight(500).margin(24) 220 List({ space: 12 }) { 221 ListItem() { 222 Text('list1').fontSize(14).fontColor('#182431').margin({ left: 12 }) 223 } 224 .width('100%') 225 .height(56) 226 .backgroundColor('#FFFFFF') 227 .borderRadius(24) 228 .align(Alignment.Start) 229 230 ListItem() { 231 Badge({ 232 value: 'New', 233 position: BadgePosition.Right, 234 style: { badgeSize: 16, badgeColor: '#FA2A2D' } 235 }) { 236 Text('list2').width(27).height(19).fontSize(14).fontColor('#182431') 237 }.width(49.5).height(19) 238 .margin({ left: 12 }) 239 } 240 .width('100%') 241 .height(56) 242 .backgroundColor('#FFFFFF') 243 .borderRadius(24) 244 .align(Alignment.Start) 245 }.width(336) 246 247 // Create a badge with the given numerical value. 248 Text('numberBadge').fontSize(18).fontColor('#182431').fontWeight(500).margin(24) 249 List() { 250 ListItem() { 251 this.itemBuilder('list1') 252 } 253 254 ListItem() { 255 Row() { 256 Image('common/public_icon.svg').width(32).height(32).opacity(0.6) 257 Badge({ 258 count: 1, 259 position: BadgePosition.Right, 260 style: { badgeSize: 16, badgeColor: '#FA2A2D' } 261 }) { 262 Text('list2') 263 .width(177) 264 .height(21) 265 .textAlign(TextAlign.Start) 266 .fontColor('#182431') 267 .fontWeight(500) 268 .fontSize(16) 269 .opacity(0.9) 270 }.width(240).height(21).margin({ left: 15, right: 11 }) 271 272 Image('common/public_icon_arrow_right.svg').width(12).height(24).opacity(0.6) 273 }.width('100%').padding({ left: 12, right: 12 }).height(56) 274 } 275 276 ListItem() { 277 this.itemBuilder('list3') 278 } 279 280 ListItem() { 281 this.itemBuilder('list4') 282 } 283 } 284 .width(336) 285 .height(232) 286 .backgroundColor('#FFFFFF') 287 .borderRadius(24) 288 .padding({ top: 4, bottom: 4 }) 289 .divider({ 290 strokeWidth: 0.5, 291 color: 'rgba(0,0,0,0.1)', 292 startMargin: 60, 293 endMargin: 12 294 }) 295 }.width('100%').backgroundColor('#F1F3F5').padding({ bottom: 12 }) 296 }.width('100%') 297 } 298} 299``` 300 301 302 303### Example 2: Controlling the Badge Visibility with Numbers 304 305This example shows how to use the **count** property to toggle the visibility of the **Badge** component. Specifically, when the **count** property is set to **0**, the badge is hidden; when it is set to **1**, the badge becomes visible. 306 307```ts 308// This example implements scaling when the badge visibility changes. 309@Entry 310@Component 311struct Index { 312 @State badgeCount: number = 1 313 314 build() { 315 Column({ space: 40 }) { 316 Badge({ 317 count: this.badgeCount, 318 style: {}, 319 position: BadgePosition.RightTop, 320 }) { 321 Image($r("app.media.startIcon")) 322 .width(50) 323 .height(50) 324 } 325 .width(55) 326 327 Button('count 0').onClick(() => { 328 this.badgeCount = 0 329 }) 330 Button('count 1').onClick(() => { 331 this.badgeCount = 1 332 }) 333 } 334 .margin({ top: 20 }) 335 } 336} 337``` 338 339 340