Android Question Spinner doesn't work with LoadLayout

Wosl

Member
Dear All,

I just wanted to test my logic when rotating the screen and created four layouts with designer (two for main and two for plot). I used the LoadLayout method to switch between the four layouts, dependent where I'm in main or plot mode and in portrait or landscape mode.

Here is the snippet of the Activity_create sub:
B4X:
Sub Activity_Create (FirstTime As Boolean)
    '---------------------------------------------------------------
    '  Fetch PV power data from SQLite DB on NAS, calculate work data and plot result
     '---------------------------------------------------------------
    PanelMainPortrait.Initialize("")
    PanelPlotPortrait.Initialize("")
    PanelMainLandscape.Initialize("")
    PanelPlotLandscape.Initialize("")
    Activity.AddView(PanelMainPortrait,  0, 0, 100%x, 100%y)
    Activity.AddView(PanelPlotPortrait,  0, 0, 100%x, 100%y)
    Activity.AddView(PanelMainLandscape, 0, 0, 100%x, 100%y)
    Activity.AddView(PanelPlotLandscape, 0, 0, 100%x, 100%y)
    PanelMainPortrait.LoadLayout("MainPanelPortrait")
    PanelPlotPortrait.LoadLayout("PlotPanelPortrait")
    PanelMainLandscape.LoadLayout("MainPanelLandscape")
    PanelPlotLandscape.LoadLayout("PlotPanelLandscape")

    If FirstTime Then
        SpinnerChartType.AddAll(SpinnerArray)
        SpinnerChartType.Color=Colors.LightGray
        SpinnerChartType.SelectedIndex = 0
        iChartTypeSelection = 0
        HeadlineMainPortrait.Text = "SQL Query of Power Values and Energy Calculation"
        HeadlinePlotPortrait.Text = "Plot Power or Works Data"
        lPlotMode = False
    End If
    ScreenHeight=Activity.Height
    ScreenWidth=Activity.Width
    If ScreenHeight > ScreenWidth Then
        '  Screen not rotated (height > width)
        lRotation = False
    Else
        '  Screen rotated (height <= width)
        lRotation = True
    End If
    ScreenPanel (True, False)
    If lPlotMode = True Then ButtonPlot_click
End Sub

The switch logic works fine but the spinner is invalid: not functioning (no selection possible) and is looking strange like this (just a triangle is visible):
Screenshot_20250326_114353.jpg



If I comment out the line
PanelMainLandscape.LoadLayout("MainPanelLandscape")
the spinner view looks as expected (the triangle is not visible because I colored the spinner background):

Screenshot_20250326_115332.jpg


Commenting out other LoadLayout commands do not have an effect. Obviously, the switching of panels do not work anymore because one panel is missing.

Any idea what is happening?

Wosl
 

Sagenut

Expert
Licensed User
Longtime User
You made the spinner population in the FirstTime block.
So it will not be executed on recreation after orientation change.
If you have the same spinner in all 4 layouts the changes will be made only on the last loaded one, so the landscape one.
You should load and populate only the layout needed due to the orientation and not loading all of them at once.
 
Last edited:
Upvote 0

Wosl

Member
Good point. I moved the spinner initialization outside the FirstTime block. I use the same spinner in all layouts and populate now the spinner while start up the app or after rotation of smartphone.

However the issue is the same because the effect appears right after starting the app (in portrait or landscape mode).
 
Upvote 0

Sagenut

Expert
Licensed User
Longtime User
Check if this can help you.
As I wrote before, if you load multiple layouts with the same view the changes will apply only to the latest ones.
Anyway you should load only the layout needed for the moment, actual orientation.
Because when there is a change of orientation the app is destroyed and rebuilded.
 

Attachments

  • Orientation.zip
    11 KB · Views: 11
Upvote 0

Wosl

Member
Thank you for your support. Your test app is running as expected.

But my solution is different:

I create four panels and load four different layouts to it: portrait vs / landscape and main vs. plot. With a simple logic you can move between the layouts easily, e.g. swap between portrait and landscape:

B4X:
    If lRotation = False Then
        '  Portrait mode activated
        PanelMainPortrait.Visible  = True
        PanelPlotPortrait.Visible  = False
        PanelMainLandscape.Visible = False
        PanelPlotLandscape.Visible = False
    Else
        '  Landscape mode activated
        PanelMainPortrait.Visible  = False
        PanelPlotPortrait.Visible  = False
        PanelMainLandscape.Visible = True
        PanelPlotLandscape.Visible = False
    End If

The spinner does not show up correctly. If I comment out the line PanelMainLandscape.LoadLayout("MainPanelLandscape") the spinner view looks as expected, but obviously the swapping between portrait and landscape is not working due to the missing panel now. The behavior is strange and could be a bug in my B4A version. May be I need to update to a newer version. My app is running with B4A version 12.20 (64 bit).

You can run my code on your machine - let's see if it is working ...
 

Attachments

  • Test3.zip
    8.1 KB · Views: 8
Upvote 0

Wosl

Member
Following actions helped to resolve the issue (spinners work as expected now):

- Update to B4A version 13.10 (was anyhow on my list)
- Define two different spinners each for portrait and landscape mode (incl. different initialization and event routines)

Thanks to @Sagenut - your hints guided me to the right path.

Wosl
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Your approach looks complicated.
Attached a modified version with tho layouts.
One Main and the other for Plot, both have two variants for portrait and landscape and using anchors.
Only one Panel for Main defined in the Designer with the different views on it.
The same for Plot.
The layouts might be improved, the goal here us to show one possibility how Android handles layouts.
 

Attachments

  • Test4.zip
    11.2 KB · Views: 4
Upvote 0

Wosl

Member
@klaus, I'm convinced about your solution. Sometimes you are too fixated on one solution and don't see the simple one. Your solution (two variants for portrait/landscape and anchor technique) avoids tricky event handling between different views.

I've not used the anchor technique before, but I'll check it out now. I found a nice tutorial created by Erel:
https://www.b4x.com/android/forum/threads/b4x-anchors-demonstrated.64112/
Of course, the layouts are poor, but the original purpose was just to check out how to handle portrait/landscape mode together with different panels.

Thank you very much for sharing your idea with me.
Wosl
 
Upvote 0

geps

Member
Licensed User
Longtime User
You could try adjusting the spinner's layout or adding a small delay (using Sleep or Wait for for layout recalculation) after loading the landscape layout to see if that resolves the issue. Another thing to consider is whether the spinner might be inheriting unwanted properties from the landscape layout, so testing the spinner in isolation (without any conflicting layouts) could help pinpoint the cause.
 
Upvote 0
Top