B4J Question [Fixed] B4xPages and center form in screen

Num3

Active Member
Licensed User
Longtime User
Good day all,

I am converting a B4J project to use B4XPages, and i used to center my forms using the CenterForm sub code:

B4X:
Sub CenterForm(f As Form)
   Dim ps As Screen = fx.PrimaryScreen
   f.WindowLeft = (ps.MaxX - ps.MinX) / 2 - f.Width / 2
   f.WindowTop = (ps.MaxY - ps.MinY) / 2 - f.Height / 2
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("pagelogin")
   
    login_user.Text = "" ' Clear user
    login_pass.Text = "" ' Clear password
   
    #if B4J
   
     Dim form As Form = B4XPages.GetNativeParent(Me)
     form.WindowWidth = 320dip
     form.WindowHeight = 190dip
     form.Icon = fx.LoadImage(File.DirAssets, "icon.png")    
     CenterForm(form) '<- Form is never screen centered
    #end if
End Sub

But that doesn't work anymore, what am i missing???
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
B4X:
    #if B4J
    Dim form As Form = B4XPages.GetNativeParent(Me)
    form.WindowWidth = 320
    form.WindowHeight = 190
    Sleep(100)
    CenterForm(form) '<- Form is never screen centered
    #end if

Note that you should write also the CenterForm Sub between #If B4J #End If
 
Upvote 1

Num3

Active Member
Licensed User
Longtime User
B4X:
    #if B4J
    Dim form As Form = B4XPages.GetNativeParent(Me)
    form.WindowWidth = 320
    form.WindowHeight = 190
    Sleep(100)
    CenterForm(form) '<- Form is never screen centered
    #end if

Note that you should write also the CenterForm Sub between #If B4J #End If
Not a problem in this case, it is a B4j only program, but you are correct
 
Upvote 0

Num3

Active Member
Licensed User
Longtime User
I've found out the problem, i was trying to center a form that was not yet shown.
The centering sub must be called in B4XPages_Appear
B4X:
Private Sub B4XPage_Appear
    'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
    Dim form As Form = B4XPages.GetNativeParent(Me)
    CenterForm(form)
End Sub
 
Upvote 0
Top