Interesting button behavior

dagnabitboy

Active Member
Licensed User
Longtime User
I have a button setup to change from black to red when I touch and release, or when I touch and move my finger off it. You can clearly see the color go from black to red twice, and logging the events confirms it. Touching the button causes down, up, down, up and click events. I was expecting to see simply: down, up and click. I'm must admit I'm a bit embarrassed to be asking such basic questions about buttons at this point, but what am I missing? Thanks!


B4X:
Sub Globals
   Dim button1 As Button
   Dim eventCounter As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
   activity.Color=Colors.LightGray
   button1.Initialize("button1")
   button1.Color=Colors.Black
   activity.AddView(button1,50dip,50dip,100dip,100dip)
   button1.Text="TEST"
   button1.TextColor=Colors.White
   eventCounter=0
End Sub
Sub button1_down
   button1.Color=Colors.Red
   Log("down")
End Sub
Sub button1_up
   button1.color=Colors.Black
   Log("up")
End Sub
Sub button1_click
   button1.color=Colors.Black
   Log("click")
   eventCounter=eventCounter+1
   Log("event: "&eventCounter)
End Sub
 

dagnabitboy

Active Member
Licensed User
Longtime User
Hi Erel; I will look into StateListDrawables, although I have many buttons to implement and was going to create them programmatically as an array. They will be more complex than what is shown in my example as I am implementing overlays of 2 panels and the button all with rounded corners to reach the desired effect. I am seeing this effect on my Vizio tablet and on both of my DroidX phones. This happens when I simply touch and release the button. If I move my finger off the button before release I simply get the final "up" response with the click event as expected, but I still get the double "down, up" response when I touch the button.
 
Upvote 0

dagnabitboy

Active Member
Licensed User
Longtime User
Hi Erel; I would like to re-visit this issue if you don't mind. What device did you try the code on? On the emulator there was no issue, only on actual devices. I've zipped up the code, perhaps someone else can give this a quick try as well. Perhaps this is an undocumented "feature". Normally, unless one has added code to change the color of a button when pressed you wouldn't see this. I've added code to log the time difference in events when I press the button. Here are a few samples:

button press timing
milisecond timing from Vizio tablet running 2.3.2
down 0
up 29
click 82 ; this was a quick tap, looks normal

down 0 ; key down
up 30 ; false up
down 96 ; 2nd down (sounds like a football game!)
up 325 ; real up
click 7 ; this is the double event I see

down 0
up 33
down 95
up 304
click 7 ; again, another double press event.

timing from a DroidX running 2.3.3
down 0
up 68
down 87
up 362
click 17

In general the Droidx timing was a bit slower and not every button press showed the problem.
 

Attachments

  • button1.zip
    5.6 KB · Views: 242
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Tested on a Samsung Galaxy Spica and it worked as expected (single down, up and click).
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I see the same behaviour on my Nexus One.

- when tuching shortly: down - up - click
- when touching, hold and release insides the button: down - up - down (hold) up - click
- when touching, hold and release outsides the button: down - up - down (hold) (moving outside button) up.

Best regards.
 
Upvote 0

dagnabitboy

Active Member
Licensed User
Longtime User
Thanks for checking Klaus; I'm glad someone else found this too. I was beginning to think I was losing my mind! I made a filter with a timer which worked well on my Vizio, but my DroidX has erratic timing in regard to the button touch events and the filter did not work. Back to the drawing board!
 
Upvote 0

Melghost

Member
Licensed User
Longtime User
Hi!
I know this thread is quite old. Sorry. I'll start a new one if you want (I actually don't know what's better)

I'm programming a keyboard. It has two buttons to move the cursor forwards and backwards. I want them to work when I push them, not later. If I hold my finger down, I want the cursor start to move fastly. I'm doing this by using Down and LongClick events.

I've found that Down event is executed twice before LongClick, just as Klaus described four years ago.

Well, it would be quite easy for me to avoid this behavior by adding some lines to my code, but I would like to know if there's an easier way to do it, and the reasons why this happens. Is it my phone's fault? I haven't seen this issue in other apps.

Failed on:
Jiayu G4S
ZTE Blade

Thank you very much.
 
Upvote 0

Melghost

Member
Licensed User
Longtime User
OK, thank you very much.

There's another easy way to do it by using Click event and a boolean. I hope I help somebody.



B4X:
'I omited irrelevant code to avoid distraction


Sub Globals
    Dim AntiDoubleDown As Boolean                'To cancel double Down event
End Sub



Sub Button1_Down
    If Not(AntiDoubleDown) Then
        DoProcess1                            'The processes we want to execute on Down event
        AntiDoubleDown=True
    End If
End Sub

Sub Button1_LongClick
    DoProcess2                                'The processes we want to execute on LongClick event
    AntiDoubleDown=False
End Sub

Sub Button1_Click
    AntiDoubleDown=False
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…