Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Facebook-like notification badges in CodenameOne

Tags:

codenameone

https://i.sstatic.net/XAUHW.png

I want to implement such notification counters for my chat app. Anyone knows how to do this in CodenameOne?

Thanks.

like image 877
Uncle Boni Avatar asked Dec 20 '25 17:12

Uncle Boni


1 Answers

See this:

    Form hi = new Form("Badge");

    Button chat = new Button("");
    FontImage.setMaterialIcon(chat, FontImage.MATERIAL_CHAT, 7);
    Label badge = new Label("33");
    badge.getAllStyles().setBorder(
            RoundBorder.create().rectangle(true));
    badge.getAllStyles().setAlignment(Component.CENTER);
    int size = Display.getInstance().convertToPixels(1.5f);
    badge.getAllStyles().setFont(Font.createTrueTypeFont("native:MainLight", "native:MainLight").
            derive(size, Font.STYLE_PLAIN));
    Container cnt = LayeredLayout.encloseIn(chat, FlowLayout.encloseRight(badge));
    cnt.setLeadComponent(chat);

    hi.add(cnt);

    TextField changeBadgeValue = new TextField("33");
    changeBadgeValue.addDataChangedListener((i, ii) -> {
        badge.setText(changeBadgeValue.getText());
        cnt.revalidate();
    });
    hi.add(changeBadgeValue);

    hi.show();

enter image description here

enter image description here

Notice that the current version of RoundBorder has a slightly larger minimum size. This was fixed in the coming update.

like image 100
Shai Almog Avatar answered Dec 23 '25 08:12

Shai Almog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!