Add view to panel via code

School Boy Error

Member
Licensed User
Longtime User
This is going to be a really simple answer I'm sure but I'm just trying to get my head round the various methods and started by trying to add a view to a panel

B4X:
Sub Activity_Create(FirstTime As Boolean)
   
   Dim pnlHeight, pnlWidth As Int
   
   pnlPlay.Initialize("pnlPlay")
   pnlPlay.Color = Colors.green
   
   pnlHeight = Activity.Height - 50
   pnlWidth = Activity.Width - 50
   
   lblPlay.Initialize("cell")
   lblPlay.Color = Colors.Blue
   
   pnlPlay.AddView(lblPlay, 50, 50, 100, 100)

   Activity.AddView(pnlPlay, 0, 0, pnlWidth, pnlHeight)

End Sub

I started off just by adding a big green panel - worked fine. Now I want to add a big blue button to my big green panel. Problem is I see the blue button but no panel behind it?!
 

admac231

Active Member
Licensed User
Longtime User
This works perfectly fine for me :S

fIx6t.png
 
Upvote 0

School Boy Error

Member
Licensed User
Longtime User
Yep, here it is. I've been playing with it though. Now the green panel appears but only after I have clicked it.

I've been trying to get it so I can drag it around with my finger but you can ignore all of that code.
 

Attachments

  • plaything.zip
    7.8 KB · Views: 306
Upvote 0

School Boy Error

Member
Licensed User
Longtime User
I'd already tried that but doesn't work....

B4X:
Sub pnlPlay_touch (Action As Int, X As Float, Y As Float)

    Log("x=" & X & ", y=" & Y)
   
   If Activity.ACTION_DOWN Then
      StartX = X
      StartY = Y
   End If 
   
   If Activity.ACTION_UP Then 
      FinishX = X
      FinishY = Y
   End If 
   
   pnlPlay.Left = pnlPlay.Left + (FinishX - StartX)
   pnlPlay.Top = pnlPlay.Top + (FinishY - StartY)
      
   
'   pnlPlay.Left = X - 50dip + pnlPlay.Left
'   pnlPlay.top = Y - 50dip  + pnlPlay.Top

'      If Activity.ACTION_MOVE
'         Left = MoveLeft0 + (MoveX0 - X)
'         Left = Max(Left, 0)
'         Left = Min(Left, TotalColumnWidth(NumberOfColumns) - Activity.Width)
'         scvAssessment.Left = -Left
'         pnlHeader.Left = -Left
'         DeltaX = X - MoveX1
'         MoveX1 = X
'      End If 

'   If Activity.ACTION_MOVE Then 
'       pnlPlay.Left=X-50dip + pnlPlay.Left    
'      pnlPlay.Top=Y-50dip + pnlPlay.Top
'   End If
   
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Below you find a working solution, but I'm not sure if it is exactly what you want.
B4X:
Sub Globals
    Dim pnlPlay As Panel
        
    Dim StartX, StartY As Int
    Dim FinishX, FinishY As Int
    Dim pnlPlayLeft, pnlPlayTop As  Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    
    Dim pnlHeight, pnlWidth As Int
    Dim LabelLeft, LabelTop, LabelWidth, LabelHeight, LabelXDelta, LabelYDelta As Int
    
    pnlPlay.Initialize("pnlPlay")
    pnlPlay.Color = Colors.green
    
    LabelLeft = 5dip
    LabelTop = 5dip
    LabelWidth = 100dip
    LabelXDelta = LabelWidth + 5dip
    LabelHeight = 70dip
    LabelYDelta = LabelHeight + 5dip

    Activity.AddView(pnlPlay, 0, 0, 100%x, 100%x)
    
    For i = 0 To 19
        Dim lblPlay As Label
        lblPlay.Initialize("cell")
        lblPlay.Color = Colors.Blue
        lblPlay.Text = "Value " & i
        lblPlay.Tag = i
        pnlPlay.AddView(lblPlay, LabelLeft + (i Mod 4) * LabelXDelta, LabelTop + Floor(i/4) * LabelYDelta, LabelWidth, LabelHeight)
    Next

End Sub

Sub Activity_Touch (Action As Int, X As Float, Y As Float)
    Select Action
    Case Activity.ACTION_DOWN
        StartX = X
        StartY = Y
        pnlPlayLeft = pnlPlay.Left
        pnlPlayTop = pnlPlay.Top
    Case Activity.ACTION_MOVE
        FinishX = X
        FinishY = Y
        pnlPlay.Left = pnlPlayLeft + (FinishX - StartX)
        pnlPlay.Top = pnlPlayTop + (FinishY - StartY)
    End Select 
End Sub
Best regards.
 
Upvote 0
Top