Programmatically AddView question

bhark54

Member
Licensed User
Longtime User
I have a program that I programattically add a panel view. I would then like to add buttons programattically that are children of the panel. Do I just panel.addview(... ?

panel1.Initialize("")
panel1.Color = Colors.Cyan
Activity.AddView(panel1,0,70%y,100%x,30%y)

panel1.AddView(btn.....

The above does not work.

If I don't make the panel and just do Activity.AddView(btn.... it works (but no panel of course).

How do I programattically do addview to create a child view?
 

raytronixsystems

Active Member
Licensed User
Longtime User
Last edited:
Upvote 0

bhark54

Member
Licensed User
Longtime User
The problem isn't not knowing how to create views programattacially it is how to make a view a child of another view.
In the listing above I can create a panel (via addview) but then I want to programattically create buttons that are children of the panel. I thought I could just do panel1.addvie(btn,.... after already successfully executing activity.addview(panel1,... ) but that does not work. The buttons do not show up. I will write a complete example after I get home from work this evening. Thanks.
 
Upvote 0

bhark54

Member
Licensed User
Longtime User
Success! Creates 30 buttons that are children of panel. Pressing button sets label text. This is part of custom keyboard logic for a flight log app I am working on. Thanks for the help.

B4X:
Sub Globals
    Dim panel1 As Panel
   Dim label1 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
panel1.Initialize("")
Activity.AddView(panel1,0,50%x,100%y,100dip)
label1.Initialize("")
Activity.AddView(label1,0,0,100%y,100)
panel1.Color = Colors.Gray
btnwidth = Activity.Width * 0.1
For row = 0 To 2
   For col = 0 To 9
      Dim btn1 As Button
      btn1.initialize("btnevent")
      panel1.AddView(btn1,col*btnwidth,row*btnwidth,btnwidth,btnwidth)
      btn1.TextSize = 7
      btn1.Text = row&col
      btn1.tag = row&col
   Next 
Next
End Sub
Sub Activity_Resume
If Activity.Width > Activity.Height Then
   panel1.Top = 45%y
   panel1.Width = 100%x
   panel1.Height = 55%y
End If
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub btnevent_Click
   Dim send As Button
   send = Sender
   label1.Text = send.tag
End Sub
 
Upvote 0

katrad

Member
Licensed User
Longtime User
Addview with a panel

I'm exactly having that problem with adding a progress bar and a label to a panel.

I initiize it first, but, I dont see the progress bar or the label?? I'm stuck. - Thanks for any help

B4X:
Dim GradBk As GradientDrawable 
   Dim Gb As GradientDrawable 
   '
   GradBk.Initialize("LEFT_RIGHT", Array As Int (Colors.RGB(238,232,170), Colors.RGB(238,221,130)))
   ProgressPanel.Initialize("Progress")
   ProgressPanel.Background = GradBk
   ProgressPanel.BringToFront 
   ProgressPanel.Visible = True
   Activity.AddView(ProgressPanel, 0, (100%y/2)-40dip,100%x,60dip)
   '
   Prog.Initialize("Progress")
   Gb.Initialize("TOP_BOTTOM", Array As Int (Colors.Yellow , Colors.RGB(184,134,11)))
   Gb.CornerRadius = 3dip
   SetProgressDrawable(Prog,Gb)
   Prog.Progress = 0
   ProgressPanel.AddView(Prog, 10dip, (100%y/2), 100%x-20dip, 5dip)
   '
   ProgLbl.Initialize("Progress")
   ProgLbl.Text = "One Moment Please..."
   ProgLbl.TextColor = Colors.Black 
   ProgressPanel.AddView (ProgLbl, 30, (100%y/2)-20dip, 240dip, 30dip)
 
Upvote 0

katrad

Member
Licensed User
Longtime User
Note that you can use 50%y instead of 100%y/2.

Maybe the progress bar is hidden by another view.

You are missing 'dip' here:
B4X:
ProgressPanel.AddView (ProgLbl, 30, (100%y/2)-20dip, 240dip, 30dip)

Thank you, I'll check that out and see if that is the problem. Thanks for the tips too!
 
Upvote 0

katrad

Member
Licensed User
Longtime User
follow up / still not showing

I get the yellow progress panel to show OK, but, the progressbar and the Proglbl do not show, I added prog.bringtofront and proglbl.bringtofront, but, it didnt help. Thanks in advance.
 
Upvote 0
Top