Android Question Get values of run time created controls in b4a

babak125

Member
hi
i created some controls such as edittext and checkbox or etc in runtime by this code:

B4X:
for i = 1 to 2
dim et as edittext
et.initialize("edittext"&i)
activity.addview(et,50dip,(i*500dip)+10dip,100dip,50dip)
next

now i need the value of controls (for example text property of edittext) in another part of my code:
B4X:
Sub btnShowEtValue_Click
' what code can i use here to show value of runtime created edittexts?
end sub

how can get this values? or any other method
thanks a lot
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
exactly it must be (based on the code in post #1)

B4X:
Sub edittext1_Click
dim et as edittext
et = sender 'sets the edittext to the element that raised the event
'do anything...
end sub
Sub edittext2_Click
dim et as edittext
et = sender 'sets the edittext to the element that raised the event
'do anything...
end sub
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
You can use the tag prpoerty during creation and then

B4X:
Sub btnShowEtValue_Click
dim v as edittext
v = Sender
Log(v.Tag)
End Sub


oh heck, I'm just not quick enough!! :(:(
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

babak125

Member
exactly it must be (based on the code in post #1)

B4X:
Sub edittext1_Click
dim et as edittext
et = sender 'sets the edittext to the element that raised the event
'do anything...
end sub
Sub edittext2_Click
dim et as edittext
et = sender 'sets the edittext to the element that raised the event
'do anything...
end sub

let me to explain more:
i have a submit button that needs the value of edittext.text for some process.how can i get edittext box outside it's event?

thanks
 
Upvote 0

babak125

Member
I am so sorry but I do not understand.
this is my code:
B4X:
Sub Activity_Create(FirstTime As Boolean)

    Activity.LoadLayout("Layout1")
    edit
End Sub
Sub edit()
    For    i = 1 To 2
        Dim et As EditText
        et.Initialize("edittext"&i)
        et.Text = "test"&i
        et.Tag = "tag" &i
        et = Sender
        Activity.AddView(et,50dip,(i * 50dip)+10dip, 100dip, 50dip)
    Next
 
End Sub

Sub btnShow_Click
    For    i = 0 To Activity.NumberOfViews -1
            If sf.Left(Activity.GetView(i).Tag,3)="tag" Then
                Msgbox(Activity.GetView(i).Tag,"Tag is: ") ' it returns tag1 & tag2
                Msgbox("?????????" ,"Value is: ") ' it must return test1 & test2 at first or anything else by user entery
            End If
    Next
 
End Sub

what I must use in ?????????? section?

thanks a lot
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
I would do it that way, with an array of EditText views:
B4X:
Sub Globals
    Private edtTest(2) As EditText    'Array of EditText views
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout1")
    edit
End Sub

Sub edit()
    Private i As Int

    For i = 0 To 1
        edtTest(i).Initialize("edtTest")    'EventName without the index
        edtTest(i).Text = "test" & i
        edtTest(i).Tag = i
        Activity.AddView(edtTest(i), 50dip, (i * 50dip)+10dip, 100dip, 50dip)
    Next
End Sub

Sub btnShow_Click
    Msgbox(edtTest(0).Text & edtTest(1).Text,"Value is: ") ' it must return test1 & test2 at first or anything else by user entery
End Sub

' if you want to use an event you can use the Sender object and the Tag property.
Sub edtTest_TextChanged (Old As String, New As String)
    Private edt As EditText
    Private Index As Int

    edt = Sender    'gets the EditText view that raised the event
    Index = edt.Tag
  
    'Now you can use either
    edt.Text = "something"
    'or
    edtTest(index).Text = "something"
End Sub
 
Upvote 0
Top