Attaching multiple events to buttons

chrisleeuk2013

Member
Licensed User
Longtime User
Hi,

I have a button on my designer and an appropriate activity to handle the buttons events. click,up and down. All this is hooked up and working fine.

However what I want to do is pass the button into a class and then 'attach' additional events for click, down and up without impacting the original events.

My class is designed to add additional functionality to all buttons in my application. I tried this code below, but break points in the sub are not fired.
What I want is a simple call to hook up the extra functionality for the button, without having to add code to each button. Not sure if this is possible?


B4X:
   Dim Obj1 As Reflector
   Obj1.Target=InButton
   Obj1.SetOnKeyListener("OnKey")
   
Sub OnKey(viewtag As Object, keycode As Int, keyevent As Object) As Boolean
    'Code to react to button...
End Sub
 

chrisleeuk2013

Member
Licensed User
Longtime User
Thanks for the very prompt response. I know your the developer of Basic4Android so the speed of your reply is excellent customer support.

I tried moving the reflector code into the activity as above but pointing directly at the buttons reference. It still does not fire the OnKey when you click the button.

Is my syntax for the above correct?

I also tried commenting out the events that handle the button normally in case these were absorbing the events but it didn't make a difference.

Not sure what else to try?
 
Upvote 0

chrisleeuk2013

Member
Licensed User
Longtime User
Ok I tried with a touch event:

B4X:
Obj1.SetOnTouchListener("Touch")

Sub Touch(ViewTag As Object, Action As Int, X As Float, Y As Float, MotionEvent As Object) As Boolean

End sub

This one works fine, however I'm not sure what to do with the parameters!

What I need to know is how to get a reference to the button. The touch event is giving me the following:

ViewTag : Empty
Action : 0
X : 213.875
Y : 49.57
MotionEvent {action=ACTION_DOWN, id[0]=0,x[0]=213.875.. etc etc}

I get a second event for action_up which will give me what I want, if I can identify the button itself.
 
Upvote 0

chrisleeuk2013

Member
Licensed User
Longtime User
Just for clarification I will be hooking up multiple buttons to the same event, but I think I can do it now.

I made a guess that ViewTag would contain the 'Tag' property of the button and it does!

I can then just:

Add an array of buttons in my class.
When I call my class add the buttons to the array
Set the buttons tag to the index in the array.
Add the Touch event.

Then:

In the Touch event grab the viewtag and lookup the button in my array undertake whatever code I need to perform.

Unless anyone can think of a simpler way!?
 
Upvote 0
Top