Android Question [Solved] B4XPages page width and height

JesseW

Active Member
Licensed User
Longtime User
Since initially asking, I've discovered how to use designer anchors, which answers my actual concern of trying to make a table or listview full screen, but it'd still be nice to know how to get the actual display area width and height. You can't access the activity from B4XMainPage, so I tried this
B4X:
'I put this in Main
public Sub MainWindowWidth As Int
    Return Activity.Width
End Sub

public Sub MainWindowHeight As Int
    Return Activity.Height
End Sub

'And tried to access in in B4XMainPage like this
dim w as int = MainWindowWdith        'this did not work
'and this
dim w as int = Main.MainWindowWidth    'this did not work either

I was seemingly successful dimming MainWindowWidth and MainWindowHeight as public process globals and initializing them in activity_create like this
B4X:
'Main
Sub Process_Globals
    Public ActionBarHomeClicked As Boolean
    Public MainWindowWidth As Int            'Added this
    Public MainWindowHeight As Int            'and this
End Sub
Sub Activity_Create(FirstTime As Boolean)
    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
    MainWindowWidth = Activity.Width        'and this
    MainWindowHeight = Activity.Height        'and this
End Sub

'B4xMainPage
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Button1 As B4XView
    Private mww As Int = Main.MainWindowWidth        'get main activity width/height
    Private mwh As Int = Main.MainWindowHeight
End Sub
Private Sub Button1_Click
    xui.MsgboxAsync("w:" & mww & ", h:" & mwh, "B4X")
End Sub
because when I typed "Main." in B4XMainPage, it brought up the two public globals in Main...

I really expected this to work, but it just returned zero (0) for both values. I'm sure with a little coaxing, there could be a way to do it, perhaps if I moved the assignments from Activity_Create to Activity_Resume, but it's getting complex and there are warnings against adding code to Main. so... as I'm writing this, I have an idea. I removed all the code I had added to Main and B4XMainPage and altered this only slightly
B4X:
'In B4XMainPage
Private Sub Button1_Click
    xui.MsgboxAsync("w:" & Root.Width & ", h:" & Root.Height, "B4X")
End Sub

and it works like a champ!

So! If anyone is interested, Root.Width and Root.Height are the working display area dimensions of the device running the app. It feels good to figure things out by myself lol
 
Last edited:

JesseW

Active Member
Licensed User
Longtime User
I just discovered another method to get a B4XPage's Width and Height properties
B4X:
'In B4XMainPage
Private Sub Button1_Click
    Dim WindowWidth As Int = 100%x
    Dim WindowHeight As Int = 100%y
    xui.MsgboxAsync("w:" & WindowWidth & ", h:" & WindowHeight, "B4X")
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…