B4J Question B4XView and Events

Marcos Alves

Well-Known Member
Licensed User
Hello all,

I'm creating a hybrid app and because that using always B4XViews instead of labels and platform specific resources. In some places I need to dinamically construct the layout using runtime created arrays of views like this:

B4X:
Dim lblPrice(itensQuant) As B4XView
                     lblPrice(itensQuant).initialize("lblPrice")
In B4A i was able to capture a click event associated to this view easily using the sub:

B4X:
sub lblPrice_click
   Dim lblPrice as label = sender
   ... (use tags to identify which label was clicked and run code)
end sub
But I noticed that a B4XView hasn't the initialize method and so I can't define the associated event. How can this be done in B4X/Hybrid code?

Does anybody know?
 

Marcos Alves

Well-Known Member
Licensed User
B4XView is not a specific type of view so it cannot be initialized.

Create the native view and cast it to B4XView:
B4X:
Dim lbl As Label
lbl.Initialize("lbl")
lblPrice.Add(lbl) 'it is usually easier to work with lists instead of arrays
Dim x As B4XView = lblPrice.Get(3)
x.Text = "abc"
Thanks @Erel !
I have one additional doubt... If I use the label object in the code will the result be hybrid or I'll need to change something to convert to B4i for example? I'm developing an app in B4J following the instructions to create an hybrid code in the maximum extension possible.

Thanks!
 

Erel

Administrator
Staff member
Licensed User
The code I posted will work in the three platforms. In other cases you can use:
B4X:
Dim x As B4XView
#If B4i
Dim tf As TextField
tf.Initialize("")
x = tf
#Else If B4J
Dim ta As TextArea
...
The more you use the designer the easier things will be. The same is true for cross platform views such as XUI Views.

You can see an example of a large project where all of the code is shared: https://www.b4x.com/android/forum/threads/b4x-b4xpages-pleroma-mastodon-client.119426/#content
 

Marcos Alves

Well-Known Member
Licensed User
The code I posted will work in the three platforms. In other cases you can use:
B4X:
Dim x As B4XView
#If B4i
Dim tf As TextField
tf.Initialize("")
x = tf
#Else If B4J
Dim ta As TextArea
...
The more you use the designer the easier things will be. The same is true for cross platform views such as XUI Views.

You can see an example of a large project where all of the code is shared: https://www.b4x.com/android/forum/threads/b4x-b4xpages-pleroma-mastodon-client.119426/#content
Thanks @Erel . I'll check!
 
Top