Android Question How to define An array of Panels click

winjiadh

Active Member
Licensed User
Longtime User
in my codes

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim pnl(6) As Panel
End Sub
Sub Activity_Create(FirstTime As Boolean)
For i = 0 To 1
For j = 0 To 2
Left = (j * 120dip) + 10dip
Top = (i * 120dip) + 10dip
p = (i*3)+j
pnl(p).Initialize("")
pnl(p).Tag = Colors.RGB(Rnd(80, 256), Rnd(80, 256), Rnd(80, 256))
pnl(p).Color = pnl(p).Tag
Activity.AddView(pnl(p), Left, Top, 110dip, 110dip)
Next
Next
end sub
How to define An array of Panels click

like this
Sub pnl_Click
Dim pnl As Panel
Dim bd As BitmapDrawable
bd.Initialize(LoadBitmap( File.DirAssets ,"bback.png"))
pnl.Background =bd
End Sub

but the B4A undo
 

LucaMs

Expert
Licensed User
Longtime User
B4X:
p = (i*3)+j
pnl(p).Initialize("pnl")
pnl(p).Tag = i & J  ' <--- You can use it to distinguish between the panels.
pnl(p).Color = Colors.RGB(Rnd(80, 256), Rnd(80, 256), Rnd(80, 256))

B4X:
Sub pnl_Click
Dim pnl As Panel = Sender
' If pnl.Tag...
Dim bd As BitmapDrawable
bd.Initialize(LoadBitmap( File.DirAssets ,"bback.png"))
pnl.Background =bd
End Sub
 
Upvote 0

winjiadh

Active Member
Licensed User
Longtime User
thank you for you help
but in the code
B4X:
Sub pnl_Click
    Dim pnl As Panel 
    For i=0 To 3
        If pnl(i).Tag=3 Then pnl=pnl(i)
    Next
    Dim bd As BitmapDrawable
    bd.Initialize(LoadBitmap( File.DirAssets ,"bback.png"))
    pnl.Background =bd
End Sub

it's wrong
how to find the Tag,an use it?
thank you
 
Upvote 0

winjiadh

Active Member
Licensed User
Longtime User
I change the code like this, but I click the panel , the Panel's background isn't changed
B4X:
Sub pnl_Click(index As Int )
    'Dim pnl As Panel 
    For i=0 To 3
        If pnl(i).tag=index Then
        Dim bd As BitmapDrawable
        bd.Initialize(LoadBitmap( File.DirAssets ,"bback.png"))
        pnl(i).Background =bd
        End If
    Next
End Sub

who can help me,thanks
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
The Tag property can contain any type of data, as you can read in the documentations ;)

In my example, I entered:
B4X:
pnl(p).Tag = i & j

Since I used the "&" operator, the content of tags becomes a string (you can also insert an array!
B4X:
pnl(p).Tag = SomeArray(i, J)
)

You can get i or j using the function SubString2:
B4X:
Sub pnl_Click
Dim pnl As Panel = Sender
Dim i As Int = pnl.SubString2(0,1)
Dim j As Int = pnl.SubString2(1,2)
...

Sender is the object that triggered the event, in this case the panel that you have "clicked" (touched).
 
Last edited:
Upvote 0

udg

Expert
Licensed User
Longtime User
In your code in post #1,
pnl(p).Initialize("")
should be
pnl(p).Initialize("pnl")
if you want a sub named pnl_click to be called on a click event.

Look at Luca's code in post #2. It's all there.
BTW, I would store in TAG just the value of p (i and j combined) since, as I understand it, you're interested in the pth element of the array rater than its position in the grid.

udg
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
In your code in post #1,
pnl(p).Initialize("")
should be
pnl(p).Initialize("pnl")
if you want a sub named pnl_click to be called on a click event.

udg

Concise and efficient. I chat too much :D
 
Upvote 0
Top