Label Gravity Inconsistencies


Well-Known Member
Licensed User
Longtime User
Ah me labels were going everywhere so I made a tiny test project which I enclose for your perusal.

If you create a label in the designer then its default Horizontal Alignment = Left and its default Vertical Alignment = CENTER_VERTICAL and you can see this when you select the label in the designer (found under the +Text Style part when you click on the + ).

In my enclosed test project I have a label and three buttons which are cunningly called Left Center and Right.

The three button's click events have these simple commands in them respectively:

label1.Gravity = Gravity.LEFT
label1.Gravity = Gravity.CENTER
label1.Gravity = Gravity.RIGHT

This doesn't however work as you'd expect.

On initial running of the application the labels text appears vertically centered and to the left which is as it should be and then when you press the center button the text appears vertically centered in the middle (what you'd expect) but then when you press the Right button the label text appears on the right but it jumps up to the top, after which it stays vertically at the top when you press left or right but jumps to the vertical center when you press center.

It would seem that Gravity.LEFT also means Gravity.TOP !
Same for Gravity.RIGHT

Also when you first load the program and the text is centered vertically then just pressing the Left button causes the text to jump to the top.
Hey! All I said was Gravity.LEFT not change the Vertical alignment! You naughty B4A!!!

The only way I have found to make it consistent is to firstly in the designer set the labels default Vertical Alignment to TOP This causes the text in the label to appear at the top of the label at the start.

And then to set the middle button's command to:
label1.Gravity = Gravity.CENTER_HORIZONTAL

Now at least the label is acting consistently, yes the text is at the top but you can design for that.

If you leave the center button as:
label1.Gravity = Gravity.CENTER
then the text starts at the top and stays at the top for left and right but dips down to the center when you click the center button.

So Gravity.CENTER seems to mean both Vertical and Horizontal Center but RIGHT and LEFT seem to mean RIGHT + TOP and LEFT + TOP

You can further test that last bit by initially setting the default Vertical Alignment of the label to BOTTOM then when you press the Left or Right button it jumps to the top.


    7.1 KB · Views: 304


Well-Known Member
Licensed User
Longtime User
Ah cool! Thanks Erel.

That's whipped them into submission and they're all lining up nicely now.