Dinamically add a view

Cableguy

Expert
Licensed User
Longtime User
Imagine I have a variable 'n', and I want to use it to add 'n' views of a particular type... how can I do it?

Like:

For x = 1 to n
Dim "EditText" & x As EditText
Next


Spendt the last hour searching the forum, but nothing similar...
 

slydog43

Member
Licensed User
Longtime User
You can have an array of views like

Sub Globals
Dim myETArray(10) As EditText
End Sub

Somewhere else

Dim myLeft As Int
Dim myTop As Int
Dim myWidth As Int
Dim myHeight As Int

myleft = 10
myTop = 10
myWidth = 200
myHeight = 50
myETArray(1).Initialize("")
myETArray(1).Text = "Hello"
Activity.AddView(myETArray(1),myLeft, myTop, myWidth, myHeight)


this works but I think the line myETArray(1).Initialize("") should include some event instead of the "" but not sure
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Thanks Erel..Just what I was looking for...
One re,aining questions, in your example, all views share the same event, wich is fine, but if all the views of one kind have the same name, how can I go trhought then to get a particullar prop, like if in the example, if all were EditText views, how houl I gather the diferent Text values and then use them?
The same aproach using a For Next loop woyuld be great, but I can figure out to go through the views...
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Make a list of the editviews.

Dim MyViews as List

In Erels example you would ad et to the list


MyViews.Add (et)


You can then loop through the list

Dim et As EditText
for i = 0 to MyViews.Size-1
et = MyViews.Get (i)
et.Text= "MyView" & i ' Or anything you want to do with the view
next

In the called event, you can find which view called it using Sender

Dim et As EditText
et = Sender
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
I like you Idea, wich leads me to a similar with proven results..I will be adding eache New "et" to an array of views...This way I can ensure the views "Type" is unchangeable, and useable..
Thanks forthe Ideas..
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
One re,aining questions, in your example, all views share the same event, wich is fine, but if all the views of one kind have the same name, how can I go trhought then to get a particullar prop, like if in the example, if all were EditText views, how houl I gather the diferent Text values and then use them?
The same aproach using a For Next loop woyuld be great, but I can figure out to go through the views...
This is exactly the reason for setting the Tag value.
In your event sub you can write:
B4X:
Dim v As EditText
v = Sender
if v.Tag = 3 Then ...
 
Upvote 0
Top