What is the correct way to change the color of an image (icon) in a Label component?

39 views
Skip to first unread message

Rubén V

unread,
Oct 25, 2020, 11:18:09 PM10/25/20
to CodenameOne Discussions
HI,

I created a little method that should change the color of the image, but can't make it happen.
In the Label component I load the image like this:
        Label lbImagenInventario = new Label ();
        FontImage.setMaterialIcon (lbImagenInventario, FontImage.MATERIAL_LOCAL_SHIPPING);

And the method is as follows:
    private void changesInventColor (Double quantity, Label lbIcon, Label lbCantidad) {
        if (quantity <0) {
            lbIcono.setIconUIID ("LabelRojo");
            lbCantidad.setText (round (amount, 2));
            lbCantidad.setUIID ("LabelRojo");
        } else if (amount> 0) {
            lbIcono.setIconUIID ("LabelVerde");
            lbCantidad.setText (round (amount, 2));
            lbCantidad.setUIID ("LabelVerde");
        } else {
            lbIcono.setIconUIID ("LabelGris");
            lbCantidad.setText (round (amount, 2));
            lbCantidad.setUIID ("LabelGris");
        }
    }

Shai Almog

unread,
Oct 25, 2020, 11:55:28 PM10/25/20
to CodenameOne Discussions
In this older API the image is created within FontImage and so the colors are coded there. If you change the UIID you need to invoke setMaterialIcon again.
If you use the newer API of Label.setMaterialIcon() it should work seamlessly.

Rubén V

unread,
Oct 26, 2020, 6:28:45 PM10/26/20
to CodenameOne Discussions
Hi,

Thanks for the reply.
From what I understood the new api also forces me to reload the icon. Would I have to create the icon with the color I want first and then use the new API?

Shai Almog

unread,
Oct 26, 2020, 10:58:38 PM10/26/20
to CodenameOne Discussions
The new API will automatically recreated the icon if you invoke setUIID.

Rubén V

unread,
Oct 26, 2020, 11:03:05 PM10/26/20
to CodenameOne Discussions
Thanks
Reply all
Reply to author
Forward
0 new messages