What is Codename One?
The platform “Codename One” offers operating system-independent development of apps for smartphones. An important component is a library for user interfaces. Codename One is a successor of LWUIT, which in turn uses concepts of the Java Swing library.
To develop a user interface, you insert the various controls such as labels, buttons or input fields in a container object. The positioning is not handled by the container itself, but by an associated object of the class layout manager. For example, you attach a Gridlayout object to position the items in a table. This is often very useful, because the Gridlayout object will automatically adjust the positions and sizes for various display sizes.
The problem
In one of my applications there is such a grid layout, and one column contains buttons. Depending on whether you use the phone in your left or right hand, you can better access buttons on the left side or on right side of the screen. I would therefore like to give users the ability to mirror the positioning of the elements.
My solution
It is very easy to swap element in a two-dimensional array . Unfortunately, this is not so simple in Codename One. The container stores its elements in a linear structure and then the layout manager sets the table structure based on the linear structure. I have found no methods for direct exchange of elements in the container API. My solution to this is the following:
grid = new Container(); grid.setLayout(new GridLayout(rows, 2)); ... private void swapGrid(){ int cnt = grid.getComponentCount(); for (int i=(cnt-1); i < 0; i-=2){ Component cmp = grid.getComponentAt(i); grid.removeComponent(cmp); grid.addComponent(i-1, cmp); } grid.animateHierarchy(800); } |
A nice extra is the animateHierachy method. It moves all the elements simultaneously in the new position.