Android Question Reference different instances of the same layout.

Tom_707

Member
Licensed User
When I load multiple instances of the same layout, and I refer to a layout View by name, all references are relative to the layout instance that was loaded last.
How can I refer to Views by name in other instances of the layout. For example:

B4X:
'Layout1 contains a label View called lblLabel

Panel1.Loadlayout("Layout1")
Panel2.Loadlayout("Layout1")

lblLabel.Text = "Some text"


This will change the Text property of the label located in the second instance of Layout1, ie. lblLabel of Panel2.
How do I change the Text property of the label in the first instance of Layout1, ie. lblLabel of Panel1?

I will be referencing many Views and I don't want to use Panel.GetView(x). Is there some way to change between layouts, or some method to access Views by name, in other instances of the layout?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
All kinds of ways to do it.

Two examples:
1. You can access the views by their index:
B4X:
Panel1.GetView(x).Text = "some text" ''Panel1 type should be B4XView

2. You can create a Map with the views:
B4X:
Panel1.LoadLayout("Layout1")
Panel1.Tag = CreateMapOfViews

Panel2.LoadLayout("Layout1")
Panel2.Tag = CreateMapOfViews

Dim m As Map = Panel2.Tag
Dim lbl As B4XView = m.Get("lblLabel")

Sub CreateMapOfViews As Map
 Return CreateMap("lblLabel": lblLabel, ...)
End Sub
 
Upvote 0

Tom_707

Member
Licensed User
Thanks Erel.
As I wrote above, I don't want to use the Panel1.GetView(x) method:
I will be referencing many Views and I don't want to use Panel.GetView(x).


Using maps is not a bad idea, but it's going to mean a lot of extra code. Do you have any other ways of doing this?

Is there a way to switch between entire layouts? So a reference to lblLabel.Text would point to the correct View in the desired layout, by making that layout 'active'.
 
Upvote 0
Top