Same Layout for different panels?

corwin42

Expert
Licensed User
Longtime User
I need an Activity with 7 Panels which all will have the same layout.

Is it possible to load the same layout to different panels? How can I access the views on the panel then? I think Erel has posted a solution for this somewhere but I didn't find it anymore.
 

warwound

Expert
Licensed User
Longtime User
Hi there.

I was experimenting just a few days back with loading a (created by Designer) layout in an activity multiple times.

To test it out i created a simple app (attached):

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim Label1 As Label
   Dim Panel1 As Panel
   Dim ScrollView1 As ScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   ScrollView1.Initialize(100%y)
   Dim i, count, spacing As Int
   count=4
   spacing=1
   For i=0 To count
      ScrollView1.Panel.LoadLayout("ScrollViewTemplate")
      Panel1.Top=(Panel1.Height+spacing)*i
      Panel1.Width=100%x
      Panel1.Tag=i
      Label1.Text="Is this 'instance #"&i&"' of Label1?"
   Next
   Activity.AddView(ScrollView1, 0, 0, 100%x, 100%y)
   ScrollView1.Panel.Height=(i*Panel1.Height)+(i*spacing)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Panel1_Click
   Log("Panel1_Click")
   Dim ClickedPanel As Panel
   ClickedPanel=Sender
   Dim Tag As Int
   Tag=ClickedPanel.Tag
   Log("Clicked Panel Tag is: "&Tag)
   
   Label1.Text="Label1.Text has now been modified"&CRLF&"Tag of clicked panel is "&Tag
End Sub

ScrollViewTemplate is a layout i created with the Designer (320x480 scale=1 only).
It is a panel that contains an ImageView and a Label.

Loading the layout multiple times works perfectly BUT the global references to each panel and child views are not useable...
As Erel pointed out these references will refer only to the last instance of the layout loaded.

Look at the Panel1_Click Sub.
When this Sub is called (sometime after Activity_Create has exited), Panel1 will be a reference to the Panel1 instance last loaded by the layout.
The Sub still functions perfectly though and it's Sender object is the clicked Panel in the ScrollView (not always the last Panel loaded by the layout).

In my app i'm creating the ScrollView Panels with data from a List so by setting the Panel Tag to the List index value i can detect which data to act upon in Panel1_Click.

B4X:
Label1.Text="Label1.Text has now been modified"&CRLF&"Tag of clicked panel is "&Tag

I added that just to confirm what Erel stated these references will refer only to the last instance of the layout loaded - look at the last Panel text in the ScrollView when you click any ScrollView Panel.

Oh for some javascript and it's anonymous function closures :sign0162:!

Martin.
 

Attachments

  • MultiLayouts.zip
    5.9 KB · Views: 281
Upvote 0

corwin42

Expert
Licensed User
Longtime User
First sorry for writing this in the wrong forum. Was a bit in a hurry today.

Thanks for your answers.

The Panels will contain many labels which text fields must be changed from code. I think I have to put a key to the labels so I can Identify the labels on the panel.
Then I can loop over all views on my panel I want to change and then identify the labels to change with their tag value.
 
Upvote 0

corwin42

Expert
Licensed User
Longtime User
You can also use arrays to hold all the views. The way to do it is by loading a layout, adding the views to the array and then loading the next layout.

Good idea. Will try that.
 
Upvote 0

StarinschiAndrei

Active Member
Licensed User
Longtime User
Hi there.

I was experimenting just a few days back with loading a (created by Designer) layout in an activity multiple times.

To test it out i created a simple app (attached):

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   Dim Label1 As Label
   Dim Panel1 As Panel
   Dim ScrollView1 As ScrollView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   ScrollView1.Initialize(100%y)
   Dim i, count, spacing As Int
   count=4
   spacing=1
   For i=0 To count
      ScrollView1.Panel.LoadLayout("ScrollViewTemplate")
      Panel1.Top=(Panel1.Height+spacing)*i
      Panel1.Width=100%x
      Panel1.Tag=i
      Label1.Text="Is this 'instance #"&i&"' of Label1?"
   Next
   Activity.AddView(ScrollView1, 0, 0, 100%x, 100%y)
   ScrollView1.Panel.Height=(i*Panel1.Height)+(i*spacing)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Panel1_Click
   Log("Panel1_Click")
   Dim ClickedPanel As Panel
   ClickedPanel=Sender
   Dim Tag As Int
   Tag=ClickedPanel.Tag
   Log("Clicked Panel Tag is: "&Tag)
   
   Label1.Text="Label1.Text has now been modified"&CRLF&"Tag of clicked panel is "&Tag
End Sub

ScrollViewTemplate is a layout i created with the Designer (320x480 scale=1 only).
It is a panel that contains an ImageView and a Label.

Loading the layout multiple times works perfectly BUT the global references to each panel and child views are not useable...
As Erel pointed out these references will refer only to the last instance of the layout loaded.

Look at the Panel1_Click Sub.
When this Sub is called (sometime after Activity_Create has exited), Panel1 will be a reference to the Panel1 instance last loaded by the layout.
The Sub still functions perfectly though and it's Sender object is the clicked Panel in the ScrollView (not always the last Panel loaded by the layout).

In my app i'm creating the ScrollView Panels with data from a List so by setting the Panel Tag to the List index value i can detect which data to act upon in Panel1_Click.

B4X:
Label1.Text="Label1.Text has now been modified"&CRLF&"Tag of clicked panel is "&Tag

I added that just to confirm what Erel stated these references will refer only to the last instance of the layout loaded - look at the last Panel text in the ScrollView when you click any ScrollView Panel.

Oh for some javascript and it's anonymous function closures :sign0162:!

Martin.

Hi ,

I tried to change my code based on you exemple. Unfortunately it doen't work. Previousely i use as a sender a label but after each click i got same value (row 19 col 5).
Could you please tell me what is wrong in my code ?

For y=0 To cur.RowCount -1
cur.Position =y
rand(0)=cur.GetInt("id")
rand(1)=cur.GetString("nume")
rand(2)=cur.Getstring("prenume")
rand(3)=cur.GetInt("nrOre")
rand(4)=cur.GetString("telefon")
rand(5)=cur.GetString("datanastere")
For i=0 To nrcol -1

l.Initialize("cell")
l.Gravity =Gravity.CENTER
l.TextSize =18
l.Text =rand(i)
l.TextColor =HeaderFontColor
Dim rc As RowCol
rc.Initialize
rc.Col =i
rc.Row =y
l.Tag =rc
SV.Panel.AddView(l,colwidth*i,rowhight*y,colwidth,rowhight)
Next
Next
SV.FullScroll(True)
SV.Panel.Height = rowhight * y
End Sub

Sub cell_click
Dim Send As Label
Dim rc As RowCol

Send = Sender
rc = Send.Tag
Activity.Title = "Row " & rc.Row & " Col " & rc.Col &" / " & Send.Text
End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I think you have to redim the label inside the for_loop:
B4X:
For i=0 To nrcol -1
dim l as label
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
panel: a views container?

Is Panel view a container of other views? I want to put for example, different
views on two panels in the same layout. how can I do that?
thanks for your help in advance.
Note: I tried it and found the views don't follow the panel on which they were
drown. (you move the panel they stay).
 
Upvote 0

StarinschiAndrei

Active Member
Licensed User
Longtime User
I think you have to redim the label inside the for_loop:
B4X:
For i=0 To nrcol -1
dim l as label

Hi ,

I redim l as label , but it works as previous time.
It always return same value. I checked in debug mod if for ... next fill the tag with diffrent value and it's ok. (attached you can see screenshot)
Sub cell_click
Dim l As Label
Dim rc As RowCol

l = Sender
rc = l.Tag
Activity.Title = "Row " & rc.Row & " Col " & rc.Col &" / " & l.Text
End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
mc73 told you to Dim l As Label in the routine where you add the labels, in your code in post# 7 you don't dim them.
B4X:
For i=0 To nrcol -1 
   Dim l As Label
   l.Initialize("cell")
   l.Gravity =Gravity.CENTER 
   l.TextSize =18
  .
  .
Best regards.
 
Upvote 0
Top