Android Question Add view by click on button

aran_ghasmi

New Member
Hello, I want to add a panel by each clicking on a button. How should I do this? Please help me ... Thank you
 

mangojack

Expert
Licensed User
Longtime User
B4X:
'globals ...
Private posLeft, posTop As Int
Private numPanels As Int = 1 

Sub Button1_Click
    Dim pnl As Panel
    pnl.Initialize("MyPanels")
    pnl.Tag = numPanels
    pnl.Color = Colors.RGB(Rnd(0, 255), Rnd(0, 255), Rnd(0, 255))
    Activity.AddView(pnl, posLeft, posTop, 100dip, 60dip)
    posLeft = posLeft + 50dip
    posTop = posTop + 50dip
    numPanels = numPanels +1
End Sub

Sub MyPanels_Click
    Dim p As Panel = Sender
    Log($"You have clicked Panel # ${p.Tag}"$)
End Sub
 

Attachments

  • Panels Example.zip
    9.1 KB · Views: 109
Upvote 0

aran_ghasmi

New Member
Thanks work very good...but now i want add 2 panle with 48%x width by one clicking... i change your code for this work But after twice press the button 2 panle add to layout and by one click no panle add
 

Attachments

  • Screenshot_20200904-184254.png
    Screenshot_20200904-184254.png
    140.5 KB · Views: 92
Upvote 0

mangojack

Expert
Licensed User
Longtime User
Firstly ... as @Brian Dean stated , if you have a problem it helps to show / paste some code. >>> [code] copy and paste your code here with the code tags [/code]

Also if you have any further questions not
directly related to your original question , please start a new thread.

B4X:
'posLeft removed from Globals

Sub Button1_Click
    Activity.AddView(CreatePanel, 5dip, posTop, 48%x, 48%x)  'height same as width
    numPanels = numPanels +1

    Activity.AddView(CreatePanel, 51%x, posTop, 48%x, 48%x)
    numPanels = numPanels +1
  
    posTop = posTop + 48%x + 5dip
End Sub

Sub CreatePanel () As Panel
    Dim pnl As Panel
    pnl.Initialize("MyPanels")
    pnl.Tag = numPanels
    pnl.Color = Colors.RGB(Rnd(0, 255), Rnd(0, 255), Rnd(0, 255))
    Return pnl
End Sub

Sub MyPanels_Click
    Dim p As Panel = Sender
    Log($"You have clicked Panel # ${p.Tag}"$)
End Sub
 

Attachments

  • Panel Example 2.zip
    9.3 KB · Views: 109
Last edited:
Upvote 0

Similar Threads

Top