Android Question Is there a reason why a Panel cannot have Gravity?

Widget

Well-Known Member
Licensed User
Longtime User
Why can't a Panel have a Gravity property? (Delphi has an Align property that makes positioning panels a breeze when the window resizes.)

This comes in handy when you need to position a panel relative to its parent panel (or Activity).

Example. If you want a panel to be at the bottom of another panel (or activity) you would use "Panel1.Gravity = Bottom". When the parent panel resizes, Panel1 will be repositioned so its bottom is always aligned to the bottom of the parent.

This will allow us to use code to simulate the Designer's panel anchors & more! We will be able to position sections of the screen much easier using code.

Examples:
B4X:
Panel1.Gravity = Bottom                          'Align Panel1 to bottom of parent panel
Panel1.Gravity = Bottom + Left + Right    'Align Panel1 so it fills bottom of parent panel (its height will not change)
Panel1.Gravity = Center                           'Align Panel1 to vertical & horizontal center of parent panel
Panel1.Gravity = Fill                                 'Align Panel1 so it fills its parent panel
Panel1.Gravity = TOP + BOTTOM + RIGHT  'Align Panel1 to right edge and height of parent (its width will not change)
Panel1.Gravity = Center_Horizontal   'Align Panel1 so it is centered horizontally within its parent
Panel1.Gravity = Center_Vertical       'Align Panel1 so it is centered vertically within its parent
If the parent panel has more than one panel with Gravity=Right, then the panels will align with each other (to prevent the panels from stacking on top of each other).

So when a Panel (or activity) is resized, the panels that it contains will be realigned automatically.


Also a Panel.Border (Rect) property would be welcomed because then we could define a gap between the Panel and what it is aligned to.

If the parent panel has more than one panel with Gravity=Right, then the panels will align with each other (to prevent the panels from stacking on top of each other).

So when a Panel (or activity) is resized, the panels that it contains will be realigned automatically.


Also a Panel.Border (Rect) property would be welcomed because then we could define a gap between the Panel and what it is aligned to.

B4X:
'Align the panel to the right with a 5dip gap from the right edge of the activity (or parent panel)
Panel1.Border.Top = 10dip   'Larger gap at the top
Panel1.Border.Right = 5dip   'Narrower gap at the right
Panel1.Gravity = Right

What do you think?
 

Widget

Well-Known Member
Licensed User
Longtime User
B4A visual designer includes many features that help you to build a flexible layout. Anchors is one of them. You should use the designer to implement the layout.

Technically it is possible to implement the "anchors feature" programmatically. For now it is not planned.

But I don't use the Designer. Because I plan to support B4A, B4i (and perhaps B4j), and I don't want to have to support several screen layouts, all of my views are initialized and positioned using code.

Of course if B4x comes out with a universal screen layout where the same Designer .bal file can be used on all 3 platforms, then I would gladly use the Designer. Is anything like that planned in the future?

TIA
 
Upvote 0

Widget

Well-Known Member
Licensed User
Longtime User
Using the designer makes it simpler to create cross platform applications as the designers of B4A, B4J and B4i are almost identical. It will be more difficult to implement the UI programmatically.

You can use the bal2bil tool to convert B4A layouts to B4i.

I didn't know there was a bal2bil tool. Thanks. :)

Can you tell me why will it be more difficult to implement the UI programmatically? I'd like to know this now before the hole I'm digging for myself gets any deeper. :oops:

When you say designing the UI using code is more difficult to implement, is this in respect to the panel gravity I was talking about earlier? Or are there other gotcha's I have to worry about when using the same code to build a UI that is compatible with B4i and B4A?

I have already written the code to initialize and position the views in my B4A app, and yes it is a pain, but once it is written it gives me a lot of control over positioning and sizing of the views. Are you saying I will run into difficulties if I try to use the same code with B4i? I was hoping to use the same rtns to create and position the views in B4i that I use in B4A. If I can't do that, then the Designer looks more inviting.

TIA
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I recommend you to read B4i beginner's guide. It does cover these topics.

As B4A and B4i both based on the platform native UI elements, the code that you will need to build the layout will be different.
The designer on the other hand is almost identical and takes good care of adopting to multiple screen sizes.

There is also a significant difference in the way that screen size changes are handled (orientation changes for example). If you use the designer then it will be handled automatically.
 
Upvote 0
Top