Android Question Assigning Parent to Panel

RonC.

Member
Licensed User
Longtime User
I'm trying to program a layout in the code without using the designer: the problem I'm having is assigning the Panel as the parent to an array of button. Is there any possible way to do this or do I have to use the designer to assign the buttons to the parent?

Want to assign buttons to the panel so I can change Enabled & Visible status of the panel... Also to simplify animation (animating the whole panel).

The other reason I'm doing this is because I believe that's it's impossible to assign the buttons in the designer to array of buttons so that when I make changes to the array, it also makes changes to the designer buttons.

Arrays are much easier to work with...

Let me know if I'm going about this the wrong way...
 

RonC.

Member
Licensed User
Longtime User
I'm getting another issue, but not on a line I expected to have a fault... java.lang.NullPointerException on pnlMain.SetLayout(0,0,100%x,100%y) I'm trying new things (experimenting), here is the code:

B4X:
Sub Globals
  Private pnlMain As Panel
  Private AdMob As Boolean : Admib = False
  Private Rows As Int : Rows = 3
  Private Cols As Int : Cols = 3
  Private btn(Rows, Cols) As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
  SetupDesign
End Sub

Private Sub SetupDesign
  Private i,j As Int
  Private k as Int : k = 1

  pnlMain.Initialize("")
  If AdMob = True Then
    pnlMain.SetLayout(0,50dip,100%x,100%y-50dip)
  Else
    pnlMain.SetLayout(0,0,100%x,100%y)
  End If

  For i = 0 To Rows - 1
    For j = 0 To Cols - 1
      btn(i,j).Initialize("btnRoll")
      btn(i,j).Tag = k
      btn(i,j).Text = k
      pnl.AddView(btn(i,j),j*(100%x/Cols), i*(100%y/Rows),(100%x/Cols),(100%y/Rows))
      k = k + 1
    Next
  Next
End Sub

Had to hand type this out, EMET doesn't let me cut and paste into browser. :(
 
Last edited:
Upvote 0

RonC.

Member
Licensed User
Longtime User
Thank you LucaMs & NJDude!
Here's the changes to the working code with fixes for AdMob in the loop:

B4X:
Sub Globals
    Private pnlMain As Panel
    Private AdMob As Boolean : AdMob = False
    Private Rows As Int : Rows = 5
    Private Cols As Int : Cols = 7
    Private btn(Rows, Cols) As Button
    Private lblTitle As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    InitCollection
    SetupDesign
End Sub

Private Sub InitCollection
    Private i,j As Int
 
    For i = 0 To Rows - 1
        For j = 0 To Cols - 1
    btn(i,j).Initialize("btnCollection")
    btn(i,j).Tag = i & "," & j
    btn(i,j).Text = i & "," & j
        Next
    Next
End Sub

Private Sub SetupLayout
    Private TrueSquare As Int
    Private TitleGapY, Gap As Int
    Private ScreenX, ScreenY As Int
    Private GapDirectionX As Boolean
    Private i,j,i1,j1 As Int : i = 0 : j = 0
    Private TempRows, TempCols As Int : TempRows = Rows : TempCols = Cols
 
    ' Init pnlMain and Init lblTitle
    ' Don't Init btn array because that should be done at startup
 
    pnlMain.Initialize("Activity")
    lblTitle.Initialize("pnlMain")

    ' Make the panel depending on admob
    If AdMob = True Then
        Activity.AddView(pnlMain,0,50dip,100%x,100%y-50dip)
        pnlMain.SetLayout(0,50dip,100%x,100%y-50dip)
    Else
        Activity.AddView(pnlMain,0,0,100%x,100%y)
        pnlMain.SetLayout(0,0,100%x,100%y)
    End If
 
    ' ScreenX & ScreenY is for calucalting the full
    ' Screen Real Estate
    ScreenX = pnlMain.Width
    ScreenY = pnlMain.Height
 
    ' Determines which way the Rows and Cols should be on the screen
    ' depends on orientation and Rows/Cols
    If ScreenX > ScreenY AND Cols < Rows Then
            TempRows = Cols
            TempCols = Rows
        Else If ScreenX < ScreenY AND Cols > Rows Then
            TempRows = Cols
            TempCols = Rows
    End If
 
    ' Figure out what a TrueSquare is on the screen
    ' Uses the pnlMain.Width & Height instead of
    ' Activity.Width & Height
    If (ScreenX/TempRows) > (ScreenY/TempCols) Then
        TrueSquare = (ScreenY/TempRows)
        Gap = ((ScreenX/TempCols) - TrueSquare)/2
        GapDirectionX = True
    Else
        TrueSquare = (ScreenX/TempCols)
        Gap = ((ScreenY/TempRows) - TrueSquare)/2
        GapDirectionX = False
    End If
 
    ' Make the Title, Subtract the title from the
    ' screen real estate, Make a Gap
    pnlMain.AddView(lblTitle,0,0,100%x,TrueSquare)
    TitleGapY = lblTitle.Height
    ScreenY = ScreenY - TitleGapY
 
 
    For i1 = 0 To TempRows - 1
        For j1 = 0 To TempCols - 1
            If GapDirectionX = False Then
                    pnlMain.AddView(btn(i,j),j1 * (ScreenX/TempCols),(i1 *(ScreenY/TempRows))+Gap+TitleGapY,ScreenX/TempCols,TrueSquare)
            Else
                    pnlMain.AddView(btn(i,j),(j1 * (ScreenX/TempCols))+Gap,(i1 * (ScreenY/TempRows))+TitleGapY,TrueSquare,ScreenY/TempRows)
            End If
            i = i + 1
            If i > (Rows - 1) Then
                i = 0
                j = j + 1
            End If
        Next
    Next
End Sub

Edit: Using a ratio of 1:1 for buttons, Label for the Top, Gap for Admob, Seperated button init code, Button names & tags show array locations... Better Code.
 
Last edited:
Upvote 0
Top