Android Question It is possible to build a View Constructor to avoid repetitive code?

valekuatro

Member
Licensed User
Hello, perhaps i´m confussed but I have to ask.
Let's say i must initialize, set properties and add to a parent several objects.
Button1, Button2, Button3, Label1, Label2, Label3, Spinner1, etc...

It is possible to do a SUB like
Constructor (objectname, param2, param3, param4,param5)
Which will initialize and add the basic properties of such objects as size, position, color, visible, etc,
and add it to a Parent (a Panel by example)?

Thanks
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Don't see why not. I would probably create a class to do it - although you might find that it's more work than just doing it in your activity code (or using the Designer).

- Colin.
 
Upvote 0

Kwame Twum

Active Member
Licensed User
Longtime User
I've implemented something similar for my labels.

B4X:
Sub SetLabelAttributes(lab As Label, tface As Typeface, fontsize As Int, textcolor As Int, align As Int)
    lab.Typeface = tface
    lab.TextSize = fontsize
    lab.TextColor = textcolor
    lab.Color = Colors.Transparent
    lab.Gravity = align
End Sub

Note that you can set defaults in this sub so that you only need to pass few parameters.
 
Upvote 0

valekuatro

Member
Licensed User
Thanks Computersmith64, however i think different. Maybe in then beginning it´s a pain, but later all it´s better. This implies much less code, less code is less errors and more clarity about the structure in general and with the use is gained the time that was lost in programming the constructor. However to be honest I do not even know where to start with classes, I'm new to basic4android

Thanks Kwame
That´s the half way, how about initialize and add to a parent? It is possible in a Sub?
I was thinking if there is a parameter that can refer to an object and if it works to initialize, something like &objectname.Initialize(objectname)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Kwame Twum

Active Member
Licensed User
Longtime User
Try this.
It's for you to understand what's possible and not necessarily what to do:

B4X:
Sub PutLabelInParent(init, tface As Typeface, fontsize As Int, textcolor As Int, align As Int, parent As Panel, coordinates As Map, txt)  ' You can ignore coordinates and put all four parameters for positioning
   Dim lab As Label
   lab.Initialize(init) 
   parent.AddView(lab, coordinates.Get("left"), coordinates.Get("top"), coordinates.Get("width"), coordinates.Get("height"))
   lab.Typeface = tface
   lab.Text = txt
   lab.TextSize = fontsiz
   lab.TextColor = textcolor
   lab.Gravity = align
End Sub

'Usage
PutLabelInParent("hello", Typeface.DEFAULT, 12, Colors.Black, Gravity.LEFT, panel1, CreateMap("left":10dip, "top":10dip, "width":50dip, "height":30dip), "Hello there")

'You can even access the events of the label created, in the Module from which you're calling PutLabelInParent.
Sub hello_Click

End Sub
 
Upvote 0

valekuatro

Member
Licensed User
Is there a way to do something like that with a global Label or EditText so i can access his value from other subs?
I was searching but I can not find a way to declare something global within a Sub
 
Upvote 0

valekuatro

Member
Licensed User
OK Thanks Don, another basic question, i'm new so please have patience.
If i have one Edit Text and One Button, both LOCALS, how can a I trap in the button_click event the value of EditText.
Maybe i'm wrong but for me this not make much sense. If I have a Panel with elements, I think all the events and properties from this elements should be accessed from the scope of that panel, or the sub who had initialized it, something like parent/child relation. I know that´s not correct, i just think aloud and repeat, i´m new to b4a, java, android.
Anyway, how can a I trap in a LOCAL button_click event the value of LOCAL EditText?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
You can trap the events the same was as with Global views. The problem with Local Views would be that they are likely to be garbage collected after the sub has completed, which is why they need to be stored as a global variable, which could be an array if you want to create them at run time. There are many examples of this on the forums already.
 
Upvote 0
Top