Android Question Managing object Panel

Nitin Joshi

Active Member
Licensed User
Hi, My APP includes several Panels around 50. I have Named panels as P01, P2, P03......
I want to make visibility True/False based on input Panel number by user. I remember, VB6 has index numbering. I would like to use similarly in my APP.
 

stevel05

Expert
Licensed User
Longtime User
Are they added with the designer or in code?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If they are added to the activity in the correct order and there is nothing else in the root of the activity i.e. they are in order in the Views Tree Tab in the designer, then you can access them via Activity.GetView(Index).

If they are not in order or there are other views in the root, then you will need to build your own index.

I would store it in an Array of Panels which you can then access by index.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If you add a number to the Tag for each panel in the designer, starting from 0 in the order you want them you can bulid the index like this:

B4X:
For Each V As View In Activity.GetAllViewsRecursive
        If V Is Panel And V.Tag <> "" Then
            Panels(V.Tag) = V
        End If
    Next

Where Panels is a global array of panels
B4X:
Dim Panels(50) As Panel

Then Do
B4X:
Panels(3).Visible = False
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
By the way... Where do I mention below code?

B4X:
For Each V As View In Activity.GetAllViewsRecursive
        If V Is Panel And V.Tag <> "" Then
            Panels(V.Tag) = V
        End If
    Next
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Just after you have loaded the layout.
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
I tried this code. No error in compiling however when APP is opened, it floats the error and APP gets close.

Error
java.lang.RuntimeException: Object should first be initialized (Panel).

I have assigned in array in sub Globals
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Have you put the code After you have loaded the layout?
B4X:
    'Load layout then

    For Each V As View In Activity.GetAllViewsRecursive
        If V Is Panel And V.Tag <> "" Then
            Panels(V.Tag) = V
        End If
    Next
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
I have written this code as below

B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("My_Layout")
    For Each V As View In Activity.GetAllViewsRecursive
        If V Is Panel And V.Tag <> "" Then
            Panels(V.Tag) = V
        End If
    Next
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Try putting it at the end of the Activity_Create Sub with a Sleep(0) before it.
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("My_Layout")

'Any other code 






    Sleep(0)
    For Each V As View In Activity.GetAllViewsRecursive
        If V Is Panel And V.Tag <> "" Then
            Panels(V.Tag) = V
        End If
    Next
End Sub
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
Still problem continues. Let me tell you more about my project. My APP is interacting with ESP8266. I am executing panel visibility code in UDP_PacketArrived Sub. Please refer below code

Please feel free to ask more details...

B4X:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("My_Layout")
    UDP_Skt.Initialize("UDP",4211,255)
    Sleep (0)
    For Each V As View In Activity.GetAllViewsRecursive
        If V Is Panel And V.Tag <> "" Then
            Panels(V.Tag) = V
        End If
    Next
End Sub

Sub UDP_PacketArrived (Pkt As UDPPacket)
    Dim pnl As Int
    UDPRcvMsg = BytesToString(Pkt.Data, Pkt.Offset, Pkt.Length,"UTF8")
 If UDPRcvMsg.Length>=13 And UDPRcvMsg.SubString2(0,10)="SetControl" Then
     pnl=UDPRcvMsg.SubString2(10,13) 'Here I get panel number to make visible
     If UDPRcvMsg.SubString(13)="1" Then 'if last char of received string is 1 then panel should be visible.
            Panels(pnl).Visible=True
        Else
            Panels(pnl).Visible=False
        End If
    End If
   
End Sub
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Have you added a tag for each panel in the designer?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
As I said in post #5, add a number to the tag field in the designer for each Panel in the order you want then to be in the Array, starting at 0. The looped code then gets the tag and puts the a reference to the pane in the correct index of the Panels array.
 
Upvote 0

Nitin Joshi

Active Member
Licensed User
total panels on my designer are 11. I assigned Dim Panels(10) as Panels however APP threw error. Later I changed to Dim Panels (11) as Panels and it worked.

So If I include one more Panel then I must change the array number?
 
Upvote 0
Top