Android Question programmatically set events

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi to all. The idea is to programmatically create a UI, without using the Designer. There are no problems about positioning the various views.. but what I miss is how to set the events. I mean: in the Designer we have the "Tools/Generate Members" take creates the appropriate code for the allowed events, but I don't see (programmatically) either how to set up more that an event in the Initialize function, or how to add appropriate code for event triggering. I did the attached "experiment", with a Label, adding it to activity and creating a Click event that.. of course .. doesn't work...
Thanks for your attention

B4X:
#Region  Project Attributes 
 #ApplicationLabel: B4A Example
 #VersionCode: 1
 #VersionName: 
 'SupportedOrientations possible values: unspecified, landscape or portrait.
 #SupportedOrientations: unspecified
 #CanInstallToExternalStorage: False
#End Region
#Region  Activity Attributes 
 #FullScreen: False
 #IncludeTitle: True
#End Region
Sub Process_Globals
 'These global variables will be declared once when the application starts.
 'These variables can be accessed from all modules.
 
 Type Controllo(Name As String, _  ' this structure contains some definitions to set up a view
  Type As Byte, _
  X As Int, Y As Int, Wid As Int,Hei As Int, _
  Col As Long, _
  Eve As Byte, _
  Txt As String, _
  Ena As Boolean, _
  Vis As Boolean, _
  Tcol As Long, _
  Gra As Byte)
End Sub
Sub Globals
 'These global variables will be redeclared each time the activity is created.
 'These variables can only be accessed from this module.
 Private ETIC,INPUT,LISTA As Byte
 Private CLICK As Byte
 Private LEFT,RIGHT,CENTER,TOP,BOTTOM,CENTERH,CENTERV As Byte
 Private NewLab As Label
 Private NewInp As EditText
 Private NewLis As ListView
End Sub
Sub Activity_Create(FirstTime As Boolean)
 'Do not forget to load the layout file created with the visual designer. For example:
 'Activity.LoadLayout("Layout1")
 
 ETIC=0 : INPUT=1: LISTA=2 ' ONLY ETIC, namely Label is used
 CLICK=0
 LEFT=0 : RIGHT=1 : CENTER =2 : CENTERH=3 : CENTERV=4
 TOP=0 : BOTTOM=1
 
 AutoTest
 
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
'------------------------------------------------------------------
' 
'-------------------------------------------------------------------
Private Sub AutoTest
 Dim Test As Controllo
 Test.Col=Colors.Green
 Test.Ena=True
 Test.Eve=CLICK
 Test.Hei=200
 Test.Wid=400
 Test.Name="LabelTest"
 Test.Type=ETIC
 Test.Txt="Prova"
 Test.Vis=True
 Test.X=10
 Test.Y=30
 Test.Gra=CENTERH
 Test.Tcol=Colors.Black
 
 ControlPositioning(Activity,Test)
End Sub
Private Sub ControlPositioning(Parent As Panel,Vw As Controllo)
 Private event As String
 Private W,H As Int
 Private fx,fy As Float
 
 W=Parent.Width
 H=Parent.Height
 
 fx=W*0.01*Vw.X
 fy=H*0.01*Vw.Y
 
 Select Vw.eve ' should be the EVENT
 Case CLICK
 event=Vw.Name & "_" & "Click"
 End Select
 
 Select Vw.Type ' only ETIC
 Case ETIC
 NewLab.Initialize(event)
 ' ADDED to "Parent", but Parent is Activity 
 Parent.AddView(NewLab,Round(fx),Round(fy),Vw.Wid,Vw.Hei)
 
 NewLab.Color=Vw.Col
 NewLab.Enabled=Vw.Ena
 NewLab.Visible=Vw.Vis
 NewLab.Text=Vw.Txt
 NewLab.TextColor=Vw.Tcol
  
 Select Vw.Gra
 Case LEFT
 NewLab.Gravity=Gravity.LEFT
 Case RIGHT
 NewLab.Gravity=Gravity.RIGHT
 Case CENTER
 NewLab.Gravity=Gravity.CENTER
 Case CENTERH
 NewLab.Gravity=Gravity.CENTER_HORIZONTAL
 Case CENTERV
 NewLab.Gravity=Gravity.CENTER_VERTICAL
 Case TOP
 NewLab.Gravity=Gravity.TOP
 Case BOTTOM
 NewLab.Gravity=Gravity.BOTTOM
 End Select
  
 Case INPUT
 Case LISTA
 End Select
End Sub
private Sub LabelTest_Click
 Dim a,b As Int
 
 b=3 ' dummy code for debug purposes
 a=b
End Sub
 

LucaMs

Expert
Licensed User
Longtime User
Qual'è lo scopo di tutto questo? Se non vuoi usare il Designer, puoi creare delle custom view col metodo vecchio, che appunto non lo usa; questo è un esempio:
https://www.b4x.com/android/forum/threads/action-list-module-creating-a-custom-view.14339/

C'era anche un thread più generico ma non riesco a trovarlo.

Comunque di sicuro devi anche passare il modulo che conterrà l'evento, la parola chiave Me nel caso dell'Activity corrente.
(nota che non hai inizializzato Test).

-----

What is the purpose of all this? If you don't want to use the Designer, you can create custom views using the old way, this is an example:
https://www.b4x.com/android/forum/threads/action-list-module-creating-a-custom-view.14339/

There was a more generic thread about how to create custom views, but I was not able to find it.

However, you must pass also the module which will contain the event routine, the keyword Me if it is this the current Activity.
(note that you have not initialized Test).



P.S. I posted a link to an example which is not exactly what I searched for your, because it uses a code module instead of a class module.
 
Last edited:
Upvote 0

KMatle

Expert
Licensed User
Longtime User
creating a Click event that.. of course .. doesn't work...

Very easy. When you initialize a view you can set the event's prefix: "MyLabel.Initialize("MyLabel")"

The events are f.e.

SUB MyLabel_Click
SUB MyLabel_LongClick

If you are not sure, just open the designer, add a view and press generate xxx as you've mentioned. You the see how the event is called. Don't save or use it.
 
Upvote 0

GiovanniPolese

Well-Known Member
Licensed User
Longtime User
Hi. By the way, I have found the reason why my example was not working. The "event" string must be composed by the name of the view only. This was my mistake. But this clarifies only one aspect of the question. In fact I don't see a solution for the fact that the Event subs must be coded at compile time. In other words, while I can programmatically define and insert the view in the activity (for example), I don't see the way to manage the events connected to that view, without writing the subs in the program. I agree that it is a strange question, but it derives from the need that I want to define the views at run time, not knowing before which views I must put in the activity. The configuration of the views should be completely dynamic. Hope to have explained. Thanks anyway.
 
Upvote 0
Top