Android Question I need an event when pressing and releasing the button

Gerardo Tenreiro

Active Member
Licensed User
I need to carry out a process when activating a button and another when releasing the button. I only see the _CLICK event on the buttons and other elements so I don't have much idea how to face this challenge

The idea is to have a button or similar on the screen and when the user presses it a code is sent, when they release it another code is sent.

I notice that CLICK is executed after the user releases the button.

How can I detect the beginning of the pulse and the end of it?

Thank you so much


Licensed User
Longtime User
If it is only for Android, you could add a Touch Event to the Button.
Either with the Reflection library or the JavaObject library.
Attached my test program.

Sub Globals
    Private Button1, Button2 As B4XView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Private r As Reflector
    r.Target = Button1.As(Button)
    Dim jo As JavaObject = Button2
    Dim e As Object = jo.CreateEvent("android.view.View.OnTouchListener", "Button2", False)
    jo.RunMethod("setOnTouchListener", Array As Object(e))
End Sub

Private Sub Button1_Touch(o As Object, ACTION As Int, x As Float, y As Float, motion As Object) As Boolean
    If ACTION = Activity.ACTION_DOWN Then
        Button1.Color = xui.Color_Red
    Else If ACTION = Activity.ACTION_UP Then
        Button1.Color = xui.Color_Blue
    End If
    Return False
End Sub

Sub Button2_Event (MethodName As String, Args() As Object) As Object
    Dim motion As JavaObject = Args(1) 'args(0) is View
'    Dim x As Float = motion.RunMethod("getX", Null)
'    Dim y As Float = motion.RunMethod("getY", Null)
    Dim Action As Int = motion.RunMethod("getAction", Null)
    If Action = Activity.ACTION_DOWN Then
        Button2.Color = xui.Color_Red
    Else If Action = Activity.ACTION_UP Then
        Button2.Color = xui.Color_Blue
    End If
    Return True
End Sub


    9.4 KB · Views: 16
Upvote 0

Gerardo Tenreiro

Active Member
Licensed User
Thank you so much

The Application is for ANDROID and IOS, I also changed all the buttons for SWIFTBOTTON and it is working well for me.
The aesthetics of the SWIFTBOTTON are somewhat different so it forces me to change all the Buttons in the APP, but it was a good solution

Thank you so much
Upvote 0