Android Question Set HorizontalCenter/VerticalCenter of view in main program

hongbii khaw

Member
Licensed User
Longtime User
Hi all,
May I know how to set the horizontalCenter/verticalCenter of view in main program?
I can find the Horizontal/Vertical Center properties in the visual designer but how can I set the value in the main program?

Thank you.
 

ronell

Well-Known Member
Licensed User
Longtime User
are you referring to text alignment?
if yes then this is the code
B4X:
label1.Gravity=Gravity.CENTER_HORIZONTAL
    label2.Gravity=Gravity.CENTER_VERTICAL
 
Upvote 0

hongbii khaw

Member
Licensed User
Longtime User
B4X:
Dim v As View
...
' find the center coordinates:
Dim cx,cy As Int
cx = v.Left + v.Width/2
cy = v.Top + v.Height

' or
' put the view there:
v.Left = cx - v.Width/2
v.Top = cy - v.Height/2
Oh i see, thank you.
Thats mean dont have a direct way to do it?
B4X:
Sub SetHorizontalCenter(v As View,HorizontalCenter As Float)
    v.Left = HorizontalCenter - (v.Width/2)
End Sub

Sub SetVerticalCenter(v As View,VerticalCenter As Float)
    v.Top = VerticalCenter + (v.Height/2)
End Sub
 
Upvote 0

BillMeyer

Well-Known Member
Licensed User
Longtime User
In your Visual Designer, Under Scripts-General you could do something like this also:


B4X:
AutoScaleAll

lblMessageProg.HorizontalCenter = 50%x
lblMessageProg.VerticalCenter = 50%y

ivLogo.HorizontalCenter = 50%x
ivLogo.VerticalCenter = 50%y
 
Upvote 0
Top