`Theme` editor and programmatic methods to set Styles use completely different units

Viewed 32

This is my first attempt to develop a mobile application on Codename One. I am working on the presentation layer currently.

I notice there is an inconsistency between the Theme editor and the programmatic set and get methods of the available options.

E.g. in the editor, I set the colors in Hex since I use Material Design Color Tool Standards. When I try to reset a Component's color programmatically, I have the option to set it using only int RRGGBB argument. Another example, I set the margin and padding units to millimeters in the Theme editor, but programmatically, there is no constant argument for millimeters option.

Please, if my assertions are wrong, leave a comment below to point my attention to it.

1 Answers

In response to your two observations:

colors in Hex and colors as Integer

Suppose you are interested in the "Light Blue 50" Material color, which hex value is: #E1F5FE. To express it as Integer, you should prefix the hex value with 0x, in this way: 0xE1F5FE. This is your Integer. See: https://www.tutorialspoint.com/hexadecimal-integer-literal-in-java

margin and padding units to millimeters

You must use the Style.UNIT_TYPE_DIPS to indicate millimeters, in this context they have the same meaning. I understand that you may be confused about that. In the javadoc (https://www.codenameone.com/javadoc/com/codename1/ui/plaf/Style.html#UNIT_TYPE_DIPS), you can read:

Indicates the unit type for padding/margin in device independent pixels. Device independent pixels try to aim at roughly 1 millimeter of the screen per DIP but make no guarantee for accuracy.

For example, to set a margin and a padding of half millimeter:

Form hi = new Form("Hi World", BoxLayout.y());
Button myBtn = new Button("My fun button 1", "Button-Red");
myBtn.getAllStyles().setMarginUnit(Style.UNIT_TYPE_DIPS);
myBtn.getAllStyles().setMargin(0.5f, 0.5f, 0.5f, 0.5f);
myBtn.getAllStyles().setPaddingUnit(Style.UNIT_TYPE_DIPS);
myBtn.getAllStyles().setPadding(0.5f, 0.5f, 0.5f, 0.5f);
hi.add(FlowLayout.encloseCenter(myBtn));
hi.show();
Related