Handle events in a Class?

raytronixsystems

Active Member
Licensed User
Longtime User
Hello!

I was reading on here that you could process Click Events inside a class just as
you could in your Activity. I'm trying to move as much code out of Main into my Classes I I can and ran into a problem processing a simple click by my class.

I am not sure if click events for Views created by the designer can be handled by a class. I tried tonight and had no luck.

I created a Panel with the Designer and put one ImageView on it. Normally I wouldDim the ImageView in Global Subs, write a click event in Main and off I am.
Here is a sample of what my non-working code class looks like:
B4X:
' module EventTestClass Code:

'Class module 
'
Sub Class_Globals
'
' this was in main Global Subs:
'
   Dim BetaImageView1 As ImageView       ' the ImageView...
'
End Sub

'Initializes the object. You can add parameters to this method if needed.
'
Public Sub Initialize
'
   BetaImageView1.Initialize("BetaImageView1")
'
End Sub

Public Sub BetaImageView1_Click
   '
   ToastMessageShow("Hello from Class Click!", True)
   CallSub (Main,"ShowDashboard") 'Display             ' 
   '
   'End of subroutine BetaImageView_Click
   '
End Sub 
'
'------------------------------------------------------------------
' module Activity Main Code:
Sub Activity_Create(FirstTime As Boolean)
   
   Dim etc As EventTestClass          ' For our event test class
   'etc.Initialize                                        ' tried here...
   
   Activity.LoadLayout("1")         'l          '                ' 
   etc.Initialize                ' and tried here too
   
   
   '
   'End of subroutine Activity_Create
   '
End Sub

I have tried putting the "etc.Initialize" code on either side of the
LoadLayout statement and get no compile or run time error, but when I
press on betaImageView1 on the Panel it never executes the "click" event
in the class. The code was working fine outside of the class until I moved it to the test class.

I put the AHdashboard code inside of a class and all is working fine but would like to move my App click events outside of Main to make my class as reusable as possible. I had just created a simple welcome screen with the designer and once the user pressed on the imageview it would invoke the Dashboard "Main Menu". I plan on replacing the Welcome screen with a Splash Screen I had coded a while back.

Any idea what is going on? I think the problem is related to the fact that the designer has generated it's own java code that is doing it's own initialization.
I have not tried creating my own Panel at run time and placing a button or imageview on the panel and handling the click events in my class. Something to try perhaps? There must be a way of handling Designer Views in a class. Any suggestions or guidance would be welcome.

Regards,
Ray
 

monki

Active Member
Licensed User
Longtime User
The Problem is you defined a New imageview in the class, this has nö Referenz to the imageview from the Designer.
 
Upvote 0

raytronixsystems

Active Member
Licensed User
Longtime User
@ Monki: The ImageView in my class is the same name as defined by the designer. I saw another example up here and it was very similar. Instead of the ImageView being declared in Global Subs it is inside of the class.

As far as the Reflection Library code, where do I put it and what is meant by
r.Target? Could you post an example based upon the sample code I put up here?

thx,
Ray
 
Upvote 0

derez

Expert
Licensed User
Longtime User
I don't think you need reflector library.
What is missing is to add the new object of the class to the panel or activity in the main.
To do this you have to define a sub in the class sub_AsView as view and to return the imageview there.
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
As far as the Reflection Library code, where do I put it and what is meant by
r.Target? Could you post an example based upon the sample code I put up here?

The goal, as I said, is to create a onClick listener to catch the click events. When you create your ImageView outside of the class (e.g. in the designer or in the activity), its events can be trapped only by the activity. That's why you need to define a second onClick listener in the class.

Look at the attached example.
 

Attachments

  • IVexample.zip
    7.2 KB · Views: 380
Upvote 0

raytronixsystems

Active Member
Licensed User
Longtime User
No Click sound in Reflection click event!!

Thanks for the help guys!

I built the example and worked well. I just incorporated Reflection into the current class I am writing and all is working well except that when my click event is executed it does not generate a "click" sound. Do I have to play a sound file in my handler or is there another way to do this?

Regards,
Ray
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Thanks for the help guys!

I built the example and worked well. I just incorporated Reflection into the current class I am writing and all is working well except that when my click event is executed it does not generate a "click" sound. Do I have to play a sound file in my handler or is there another way to do this?

With the reflection library, try the method "playSoundEffect".
 
Upvote 0

raytronixsystems

Active Member
Licensed User
Longtime User
process ore than 1 click event using reflection in a class?

Thanks for the info on using reflection to generate the click sound. I.ll try it oit later today.
Another qurstion. I tried capturing a click event from another ImageView in my class. I created a second reflection object r1 and set up anothet click handling subroutinre in the same manner as the first ImageVie but no luck. Only the first one works. How cam I use Rlection to handle clicks from multiple ImageViews in my class?
Any help Would be appreciated!
Regards,
Ray
 
Upvote 0
Top