Android Question Portrait/Landscape Variants on Main layout [Solved]

Roger Daley

Well-Known Member
Licensed User
Hi All

I am trying to add a Landscape variant to an existing App. The user chooses Portrait or Landscape as a setting, the default is portrait.
Been through the pain of creating the variant and now failing to get it to work.

The App starts in Portrait and all is good, click on the button to toggle to landscape it sort of works. [I need to do more work on the layout]

This Is The Question.
Click on the toggle button to go back to Portrait which it does but the layout is scrambled. What Happened?

Below is the essence of the code.

Toggle Portrait and Landscape:
Sub Process_Globals
    Private Phone1 As Phone
    Private ScrnOrientVal As Int    '1 = Portrait, 0 = Landscape
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    ScrnOrientVal = StateManager.GetSetting2("ScrnOrientVal", 1)
    Phone1.SetScreenOrientation(ScrnOrientVal)
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    StateManager.SetSetting ("ScrnOrientVal",ScrnOrientVal)
    StateManager.SaveSettings                                     'Saves the settings above
End Sub

Sub PortLand_Click
    ScrnOrientVal = 1 - ScrnOrientVal            'Toggles ScrnOrientVal between 1 and 0
    Phone1.SetScreenOrientation(ScrnOrientVal)    '1 = Portrait, 0 = Landscape
End Sub
It looks straight forward but something is missing.

Regards Roger
 

Roger Daley

Well-Known Member
Licensed User
LucaMs,

Thanks for the feedback. The puzzle deepens, the display is not entirely scrambled
On the first change Portrait to Landscape the screen orientation changes but it is still trying to use the Portrait variant.
On the change back to Portrait it is trying to use the Landscape variant. It seems to be out of step.

I've attached the main.bal file and the screenshots.

Regards Roger
 

Attachments

Roger Daley

Well-Known Member
Licensed User
LucaMs,

You are right it is complex and there is very lengthy script to position the views. A few by x&y the rest referenced to those.

I wasn't expecting anyone to go through the whole detail. I was expecting that my methodology was wrong and someone who knows what they are doing would spot it.

To restate and simplify the problem.
When I switch to landscape it tries to use the portrait variant and vice versa.
How do I force it to use the correct variant?

Perhaps I need to start a new post with that question. How to force the use of a variant.
 

LucaMs

Expert
Licensed User
You are right it is complex and there is very lengthy script to position the views.
Sorry, I had only looked at the general script and I thought there was nothing else, instead there are the specific scripts.

Later (!) I'll try to look carefully at the layout - unfortunately I don't have PNGs, but we'll see if ...!
 

Roger Daley

Well-Known Member
Licensed User
LucaMs

PNGs attached. Not really sure how they help but I've tried every weird idea I could think of without success. B4A just want to use the wrong variant and there does not seem to be any option to override it.

Regards Roger
 

Attachments

LucaMs

Expert
Licensed User
PNGs attached. Not really sure how they help
The PNGs would have been necessary for me to load the layout; unfortunately I had to look for each assignment in the Designer and delete it :(:)

B4A just want to use the wrong variant and there does not seem to be any option to override it.
It is not necessary to attempt an override, you have seen that in the test project that I have attached your code works.

Unfortunately, when I run the project with your layout it appears "confused", wrong from the beginning (portrait) in terms of the size of the views and their TextSizes.

Maybe you could

I am having a doubt. In the variant portrait if I'm not mistaken (I will reopen the project "soon") you have a couple of panels on the right; this fact I think influences the choice ... uhm, no, what matters is the size of the variant, based on these the most suitable is used.
 

Roger Daley

Well-Known Member
Licensed User
LucaMs

The complete project attached, I have been posting the source code for years as I update it so it's no secret. Due to file size the TTF file is in a separate zip and needs to be added to the FILES folder

It will make more sense if you are keen to look at the detail. [This was my first B4A project so contains many fine examples of how not to do things]

Regards Roger
 

Attachments

Roger Daley

Well-Known Member
Licensed User
For such complex layout, i would create layout separated by its orientation, so there are layout for portrait and landscape, and when app start it will load layout by it state.
rraswisak,

I have created 2 layouts with interesting results. In the code below if I don't set the orientation in ActivityCreate it starts as Portrait and when I rotate the phone the layout changes to Landscape [I need to tidy up to be sure of all is correct] and switches back to Portrait OK. This not what I want of course, I want the user to set the orientation and have it stay that way.

I need to play around with the code and if need be re-ask the question so I will put this question O

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("main")
    ScrnOrientVal = StateManager.GetSetting2("ScrnOrientVal", 1)
    'Phone1.SetScreenOrientation(ScrnOrientVal)
    If ScrnOrientVal = 1 Then

        Activity.LoadLayout("portrait")
        ScrnOrientVal = 0
    Else
        Activity.LoadLayout("landscape")
        ScrnOrientVal = 1
    End If
End Sub

Sub PortLand_Click

    If ScrnOrientVal = 0 Then
        Phone1.SetScreenOrientation(ScrnOrientVal)
        Activity.LoadLayout("landscape")
        ScrnOrientVal = 1
    Else
        Phone1.SetScreenOrientation(ScrnOrientVal)
        Activity.LoadLayout("portrait")
        ScrnOrientVal = 0
    End If
End Sub
 

emexes

Well-Known Member
Licensed User
B4A just want to use the wrong variant and there does not seem to be any option to override it.
Yeah, the layout variant selection is pretty good usually, but... as I understand it, the decision is made based on both size and aspect ratio, and sometimes the weighting of those two criteria does not match the programmer's. I have often done the separate layout thing too.
 

rraswisak

Active Member
Licensed User
Roger, this is what i have done;

1. i duplicate main.bal with other file name say main1.bal
2. i remove landscape variant in main.bal, and so i remove potrait variant in main1.bal
3. change activity_create with:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    ScrnOrientVal = StateManager.GetSetting2("ScrnOrientVal", 1)
    If ScrnOrientVal = 0 Then
        Activity.LoadLayout("main1") 'landscape
    Else
        Activity.LoadLayout("main") 'portrait
    End If
   
    Phone1.SetScreenOrientation(ScrnOrientVal)
   
    Canvas1.Initialize(Activity)
    ...
    ...
End Sub
4. no change with PortLand_Click sub, since orientation change will raise Activity_Create event and load correct layout
5. i change script in main1.bal to arrange the buttons

This is how it look on my real-device;
1580791799009.png


i believe there is another part which should be adapt with landscape layout like about, help, review etc...etc... and you know what it is :)

*the project EXCLUDE font file, so you must add manually
 

Attachments

Last edited:

Roger Daley

Well-Known Member
Licensed User
Roger, this is what i have done;

1. i duplicate main.bal with other file name say main1.bal
2. i remove landscape variant in main.bal, and so i remove potrait variant in main1.bal
3. change activity_create with:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    ScrnOrientVal = StateManager.GetSetting2("ScrnOrientVal", 1)
    If ScrnOrientVal = 0 Then
        Activity.LoadLayout("main1") 'landscape
    Else
        Activity.LoadLayout("main") 'portrait
    End If
  
    Phone1.SetScreenOrientation(ScrnOrientVal)
  
    Canvas1.Initialize(Activity)
    ...
    ...
End Sub
4. no change with PortLand_Click sub, since orientation change will raise Activity_Create event and load correct layout
5. i change script in main1.bal to arrange the buttons

This is how it look on my real-device;
View attachment 88180

i believe there is another part which should be adapt with landscape layout like about, help, review etc...etc... and you know what it is :)

*the project EXCLUDE font file, so you must add manually
rraswisak,

Thanks for the feedback. With your changes all seems to work
I will keep working on it to tidy up.

Yes there are a lot of things to change after I get this bit sorted.

Roger
 
Top