Documentation / Widgets / Ready-made / Notification badge

Notification badge

An icon or an image with a mark on its corner, as a phone puts on an app: a count, or a plain dot - unread messages, alarms waiting, jobs in a queue.

How the mark comes and goes. The badge keeps one number, its count. The icon is always drawn. The mark is drawn while the count is above 0 and is gone at 0. So making the mark appear, changing what it says and clearing it are all one thing: changing the count. Count comes from, at the top of the badge’s settings, says who does that:

In Dot mode the count is still what decides it - any count above 0 shows the dot. Nothing is redrawn while the count stays the same. To hide the whole badge, icon and all, that is its Hidden flag, as on any widget.

Group: Ready-made · Default size: 48 × 48 · Style parts: Main, Icon or image, Mark

A dropped Notification badge: a bell icon with a red circle on its top right corner saying 3.
A Notification badge as it is dropped: the bell, and a count of 3.
Notification badge settings: Shows An icon, Icon Bell, Icon size 32, Count comes from A number set here, Count 3, Mark The count, Counts up to 99, Mark sits Top right, Nudge X and Y.
Its settings.
SettingDefaultWhat it does
ShowsAn iconAn icon from the icon library, or one of your images.
Icon / Icon sizeBell, 32 pxWhich icon, and its size. It is drawn in the Icon or image part's text colour.
Image(none)The image, when it shows one. It is drawn at its own size, centred.
Count comes fromA number set hereWho sets the count: the badge itself, a variable, or a list’s length. See above.
Count3What the mark says to begin with, when the number is set here. 0 starts with no mark.
MarkThe countThe count as a number, or a dot that says only that there is something.
Counts up to99Above this the mark says so with a plus: 99+.
Dot size10 pxThe dot, when the mark is one.
Mark sitsTop rightWhich corner of the widget the mark is on.
Nudge X / Y0Moves the mark from its corner, in pixels.

The mark is red with white figures, round for one figure and a pill for more. Its colour, font, padding, border and shape are the Mark part in Style settings, and the icon's colour or the image's recolour is the Icon or image part. In your own code the count is ui_badge_set(ui_Badge1, 5), ui_badge_add(ui_Badge1, 1) for one more and ui_badge_get(ui_Badge1) to read it; ui_badge_set(ui_Badge1, 0) clears the mark. In a MicroPython export they are PT.badge_set(ui.ui_Badge1, 5), PT.badge_add and PT.badge_get.

Example: a count that buttons change

  1. Drag a Notification badge onto the screen and name it Inbox. Under its settings choose the Mail icon and set Icon size to 42; give the widget a Width and Height of 56.
  2. Drag a Button saying New message. Give it an event: Clicked, Action Set badge count, Notification badge Inbox, Does Adds to the count, By 1.
  3. Drag a second button saying Read all, with the same action and Does set to Clears it: no mark.
  4. Press Preview. Two clicks on New message make the 3 a 5; Read all takes the mark away.
Preview: an envelope icon with a red 5 on its corner, and the buttons New message and Read all under it.
After two clicks on New message.
The New message button's Events: Clicked, Set badge count, Notification badge Inbox, Does Adds to the count, By 1.
Each click adds one.