Can B4A do repeat buttons?

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

I have some buttons for volume up and volume down.

I would like to make these repeat buttons when the user presses one them and holds down on the button.

Can you show me the coding needed to accept a long press and have the button call a sub routine over and over until the user lets go of the button?

Thanks.
 

rleiman

Well-Known Member
Licensed User
Longtime User
Hi Everyone,

Thanks klaus for the guidance.

Here's what I came up with:

I set the timer here.
B4X:
Sub Activity_Resume

   ' Restore the state this activity was in prior to exiting it.
   '------------------------------------------------------------
   StateManager.RestoreState(Activity, "Main", 0)

   ' This timer is for the volume up and down buttons.
   ' The user can hold down on them to make the SeekBar
   ' value go up and down.
   '---------------------------------------------------
   tmrVolumeRepeat.Initialize("tmrVolumeRepeat", 50)
   
End Sub

These are for the pressing of the up and down buttons.
B4X:
Sub ButtonVolumeDown_Down
   
   blnVolumeDownIsHeldDown = True
   tmrVolumeRepeat.Enabled = True
   
End Sub

Sub ButtonVolumeDown_Up
   
   blnVolumeDownIsHeldDown = False
   tmrVolumeRepeat.Enabled = False
End Sub

Sub ButtonVolumeUp_Down
   
   blnVolumeUpIsHeldDown = True
   tmrVolumeRepeat.Enabled = True
   
End Sub

Sub ButtonVolumeUp_Up
   
   blnVolumeUpIsHeldDown = False
   tmrVolumeRepeat.Enabled = False
End Sub

Here is the timer event.
B4X:
Sub tmrVolumeRepeat_Tick
   
   If blnVolumeDownIsHeldDown Then

      ' Decrease seek bar value.
      '-------------------------
      SeekBarVolume.Value = SeekBarVolume.Value - 1
   Else

      ' Increase seek bar value.
      '-------------------------
      SeekBarVolume.Value = SeekBarVolume.Value + 1
   End If   
End Sub

I works most of the time but sometimes it takes several seconds for the SeekBar to move. Not sure why, but I hope my users will like this new feature to fine tune the SeekBar position with buttons.

If anyone knows how to make the movement and response smooth please show me what else to do to my coding.

Erel,
Maybe a repeat event would be nice in the next version of B4A ?

Thanks again.
 
Last edited:
Upvote 0

touchsquid

Active Member
Licensed User
Longtime User
I tried this method and it works perfectly if what you do in the timer tick event is very short. But if you do something substantial the button "sticks" in the pressed state and the up event never occurs.

Any workarounds for this?
 
Upvote 0
Top