Android Code Snippet [B4XPages] Lock screen orientation based on how device is held when App is started.

I have an App that can handle both orientations, so I wanted the user's preference to be reflected in how the App was oriented.
I searched the Forum, but I couldn't find what I needed.

In "Main" Activity prior to initializing page manager do this.
B4X:
    'set...     #SupportedOrientations: unspecified

Sub Activity_Create(FirstTime As Boolean)
    'set...     #SupportedOrientations: unspecified
    Dim ph As Phone
    If Activity.width < Activity.Height Then ph.SetScreenOrientation(7) Else ph.SetScreenOrientation(6)

    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
End Sub
 
Last edited:

Kevin Hartin

Active Member
Licensed User
I have an App that can handle both orientations, so I wanted the user's preference to be reflected in how the App was oriented.
I searched the Forum, but I couldn't find what I needed.

In "Main" Activity prior to initializing page manager do this.
B4X:
    'set...     #SupportedOrientations: unspecified

Sub Activity_Create(FirstTime As Boolean)
    'set...     #SupportedOrientations: unspecified
    Dim ph As Phone
    If Activity.width < Activity.Height Then ph.SetScreenOrientation(7) Else ph.SetScreenOrientation(6)

    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
End Sub
Hi William,

I assume that your post was for a working solution with B4Xpages. I have tried it but it seems to be stuck on Portrait.

I have a POS app that is much better in portrait on phones, small screens, etc, but on a 7 or 10 inch tablet would be better in Landscape.

I assume that I need to create different layouts in the designer in order for this to work?

When I set #SupportedOrientations: unspecified I get a log message to "#SupportedOrientations attribute must be set to landscape or portrait."

Cheers,
Kev
 

William Lancee

Active Member
Licensed User
This solution works here. Make sure the statement committing the orientation occurs before the page manager is initialized.

Ignore the log message.

Specific layouts? It depends on how much real estate the app takes. If everything is limited to a square with min(root.width, root.height)
the same layout will work in both orientations. I try to work with that and have two layouts with only the area outside the square changed.

An extract of one of my projects illustrates this (obviously I do use some custom classes and utilities):
B4X:
    PD.LoadPage("MoodTrackingPage", MoodTrackingPage)
    Wait For Layout_Ready
   
    MoodCircle = MoodTrackingPage.Get("MoodCircle")
    Dim FromLabel As VElement = MoodTrackingPage.Get("FromLabel")
    Dim ToLabel As VElement = MoodTrackingPage.Get("ToLabel")
    Dim FromTime As VElement = MoodTrackingPage.Get("FromTime")
    Dim ToTime As VElement = MoodTrackingPage.Get("ToTime")
   
    'orientation specific design - depends on screen aesthetics (balance, white space, etc.)
    If Root.Height < Root.width Then
        MoodCircle.CenterOn(1.02 * MoodCircle.Width/2, Root.height/2)
        FromLabel.MoveTo(MoodCircle.Left + .93*MoodCircle.Width, FromLabel.Top)
        ToLabel.MoveTo(MoodCircle.Left + .93*MoodCircle.Width, ToLabel.Top)
        FromTime.MoveTo(MoodCircle.Left + .95*MoodCircle.Width, FromTime.Top)
        ToTime.MoveTo(MoodCircle.Left + .95*MoodCircle.Width, ToTime.Top)
    Else
        MoodCircle.CenterOn(Root.width/2, 1.02 * MoodCircle.Height/2)
        MoodCircle.ColorAndBorder(clr.get("red"), 0, 0, 0)
        FromLabel.MoveTo(.1*MoodCircle.Width, MoodCircle.Top + .93*MoodCircle.Height)
        ToLabel.MoveTo(Root.Width / 1.7, MoodCircle.Top + .93*MoodCircle.Height)
        FromTime.MoveTo(.12*MoodCircle.Width, FromLabel.Top + FromLabel.height)
        ToTime.MoveTo(Root.Width / 1.7 + .02*MoodCircle.Width, FromLabel.Top + FromLabel.height)
    End If
   
    MoodCircle.ColorAndBorder(clr.get("red"), 0, 0, 0)
    MoodCircle.LoadCanvas("moodcircle.png", 90)                        'adds 4 canvases and 4 layers
    AnnotateMoodCircle
    FromLabel.LoadLabel($"[TextSize=-2][Alignment=Center][b]FROM[/b][/Alignment]${CRLF}[Alignment=Center]approximate time[/Alignment][/TextSize]"$)
    ToLabel.LoadLabel($"[TextSize=-2][Alignment=Center][b]TO[/b][/Alignment]${CRLF}[Alignment=Center]ongoing ${Chr(0x2192)} blank[/Alignment][/TextSize]"$)
    FromTime.LoadDateTime("time", "")
    ToTime.LoadDateTime("time", "")
   
    '    MoodTrackingPage.showLayout
    MoodTrackingPage.AddExitButton
 
Top