Android Question Create new view as a copy of a designed view.

Martincg

Member
Licensed User
Longtime User
I have designed a panel with labels, buttons and stuff. I want 7 of these panels and I would like to be able to have an array of panel so that I can deal with them easily in my program. I know I could create everything in the program but it is easier to get one made with the designer.

Say the panel I designed is DesignPanel. What I would like to do is something like this.-
B4X:
Dim aPanel(7) as Panel
Dim ii as int

for ii = 0 to 6 
   aPanel(ii).features = DesignPanel.features
   aPanel(ii).top = ii* DesignPanel.Height
Next

What should I be looking up to learn how to do that?
I see that there is Panel.LoadLayout but I can't find the reverse procedure.
 

stevel05

Expert
Licensed User
Longtime User
You could load the same layout into each panel and add each one to an array. The problem you will get is with the Global assignments for the Views, although I suppose you could also deal with those using Arrays and a current panel index.

I think it would be simpler to create a new activity to host each panel. Any shared code (apart from callbacks from the views) could go in a code module. You could also call subs in the code module from the View callbacks providing they don't interact with other views.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Since you like to work with the designer, this is my approach:
1. Create the Design panel DesignPanel. Create all its labels, buttons, etc inside it.
2. In the designer, you right click the panel and choose 'Duplicate selected Views'
3. Repeat for all 7 panels. All the child views will automatically duplicate.
4. Then in code in Globals
B4X:
Dim DesignPanel As Panel
Dim Panel1, Panel2, Panel3, Panel4,Panel5, Panel6,Panel7 As Panel
Dim MyPanels() As Panel

You can still include all 7 in an array of panel. You MUST insert the below line after you load the layout that houses the panels.:
B4X:
MyPanels=Array As Panel(Panel1, Panel2, Panel3, Panel4,Panel5, Panel6,Panel7)
You can also include the DesignPanel in the array
 
Last edited:
Upvote 0

Martincg

Member
Licensed User
Longtime User
Thanks for your replies but I am looking for something different.
If I use the designer then it becomes very difficult to keep track of views. Maybe panel1, panel2, panel3 is ok but in my experiments using the designer the corresponding labels on 3 copied panels were label 14, label30, label52 so it doesn't seem easy to keep track of things.
I understand your suggestion Mahares, but suppose instead of 7 the program needs 10 of these panels with all its buttons, edittexts, radio buttons and so on? I don't want to make as many panels in the designer as the maximum I think the user will need.


Maybe there is no simple way to copy a view and the only way to do it is for each view
B4X:
for ii = 0 to NumberOfPanels
 makeButtoncopy(DesignBtn1,Btn1(ii), Panel(ii))
 makeEditTextCopy(...
.
..
Next
[code/]
and have subs where I set all the features of the array view to be the same as the designed view (width, height colours, borders, text size...). To me that is a bit painful o_O.

The StateManager makes me think that there might be a way to copy a view, but maybe I'm a bit optimistic.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…