Android Question Views on a panel only show when LEFT aligned

Didier9

Well-Known Member
Licensed User
Longtime User
I am trying to use panels to layout the controls such that my app will look good in landscape and portrait mode.
So I located the views on two layouts, and I created two panels, one loaded with each layout, the idea being to place the panels vertically one above the other in portrait and horizontally side by side in landscape.
The layout of the panels on the screen runs fine (I set different backgrounds on the panel to check).

However, the only views that show up on the panels are those that are LEFT aligned in the Designer. Those that are BOTH or RIGHT aligned do not show at all, I suspect they fall outside the physical dimensions of the panel.
Any suggestion how to fix this, or a better method altogether?
I want to avoid having to locate each view individually, which is what I was doing before and is very hard to maintain across different screen sizes.

Here is the code I use in the activity:

B4X:
Sub Globals
    Private pnlPage1, pnlPage2 As Panel   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim pnlWidth, pnlHeight, pnl2Top, pnl2Left As Int

    If 100%y > 100%x Then
        ' Portrait
        pnlWidth = 100%x
        pnlHeight = 50%y
        pnl2Top = pnlHeight
        pnl2Left = 0
    Else
        ' landscape
        pnlWidth = 50%x
        pnlHeight = 100%y
        pnl2Top = 0
        pnl2Left = pnlWidth
    End If
   
    pnlPage1.Initialize( "" )
    pnlPage1.LoadLayout( "Panel1" )
    Activity.AddView( pnlPage1, 0, 0, pnlWidth, pnlHeight )
    pnlPage1.Visible = True

   
    pnlPage2.Initialize( "" )
    pnlPage2.LoadLayout( "Panel2" )
    Activity.AddView( pnlPage2, pnl2Left, pnl2Top, pnlWidth, pnlHeight )
    pnlPage2.Visible = True
End Sub
 

klaus

Expert
Licensed User
Longtime User
It would be easier for us to help you if you posted a small project showing the problem.
So we could what exactly you have done with the whole project, not only a code snippet.
You should set the layout in the Designer Scripts instead of loading the Panels in the code.
 
Upvote 0

RandomCoder

Well-Known Member
Licensed User
Longtime User
Are you sure you have these the right way round?
B4X:
    If 100%y > 100%x Then
        ' Portrait
        pnlWidth = 100%x
        pnlHeight = 50%y
        pnl2Top = pnlHeight
        pnl2Left = 0
    Else
        ' landscape
        pnlWidth = 50%x
        pnlHeight = 100%y
        pnl2Top = 0
        pnl2Left = pnlWidth
    End If
I would have expected portrait to have pnlWidth = 50%x and pnlHeight = 100%y and the opposite for landscape.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
It would be easier for us to help you if you posted a small project showing the problem.
So we could what exactly you have done with the whole project, not only a code snippet.
You should set the layout in the Designer Scripts instead of loading the Panels in the code.

I was not sure how to upload the designer files to the forum.
Let me know if that works.
 

Attachments

  • TestPanels.zip
    369.1 KB · Views: 271
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Are you sure you have these the right way round?
B4X:
    If 100%y > 100%x Then
        ' Portrait
        pnlWidth = 100%x
        pnlHeight = 50%y
        pnl2Top = pnlHeight
        pnl2Left = 0
    Else
        ' landscape
        pnlWidth = 50%x
        pnlHeight = 100%y
        pnl2Top = 0
        pnl2Left = pnlWidth
    End If
I would have expected portrait to have pnlWidth = 50%x and pnlHeight = 100%y and the opposite for landscape.

That part actually works. In portrait, I want each panel to be full width and half the height of the display, and the opposite in landscape.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
It would be easier for us to help you if you posted a small project showing the problem.
So we could what exactly you have done with the whole project, not only a code snippet.
You should set the layout in the Designer Scripts instead of loading the Panels in the code.

I have not managed to get familiar with the designer scripts yet. That should probably be on the list at some point...
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
I noticed this in the log:
B4X:
Panel size is unknown. Layout may not be loaded correctly.

That's what I was thinking, but I have tried to set the panel size before loading the layout but that does not help.
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
if I try this:
B4X:
    pnlPage1.Initialize( "" )
    pnlPage1.Width = pnlWidth
    pnlPage1.Height = pnlHeight
    pnlPage1.LoadLayout( "Page1" )
I get that:
Error occurred on line: 68 (Main)
java.lang.NullPointerException: Attempt to write to field 'int android.view.ViewGroup$LayoutParams.width' on a null object reference
at anywheresoftware.b4a.objects.ViewWrapper.setWidth(ViewWrapper.java:136)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:708)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:337)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:247)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:134)
[......]
If I try to put the Width and Height statements after the LoadLayout, it does nothing.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In the code in post #1 move the lines
pnlPagex.LoadLayout( "Panelx" )
after
Activity.AddView...
like this:
B4X:
pnlPage1.Initialize( "" )
Activity.AddView( pnlPage1, 0, 0, pnlWidth, pnlHeight )
pnlPage1.LoadLayout( "Panel1" )

pnlPage2.Initialize( "" )
Activity.AddView( pnlPage2, pnl2Left, pnl2Top, pnlWidth, pnlHeight )
pnlPage2.LoadLayout( "Panel2" )
 
Upvote 0

Didier9

Well-Known Member
Licensed User
Longtime User
Thank you Klaus, that did it. It gets me very close to what I was expecting.
Now I need to learn about the designer scripts for fine tuning.
 
Upvote 0
Top