Android Question Using Long Click to Increment Volume?

Taylor Chapman

Member
Licensed User
Longtime User
Hi there,

I currently have a "volume up button" on my app and it increases the volume by one each time it's clicked. I would like to have it so when the user holds down the button (ie. a long click) it begins to increment the volume by one every 500 milliseconds until the user releases it.

I'm not really sure how to approach this because the "long click" event only runs once. Also, I'm not sure how to test if the button is currently being pressed by the user.

Any suggestions would be very much appreciated!
Thanks,
Taylor
 

stevel05

Expert
Licensed User
Longtime User
Use a timer set to 500ms and in Button_LongClick sub enable the timer, in Button_Up sub, disable the timer. In the Timer_Tick sub do your volume control.
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Dim Timer1 As Timer
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("1")  'Layout just contains Button1
    Timer1.Initialize("Timer1",500)
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)
     Timer1.Enabled = False
End Sub



Sub Button1_LongClick
    Timer1.Enabled = True
End Sub
Sub Button1_Up
    Timer1.Enabled = False
End Sub

Sub Timer1_Tick
    Log("Tick")
    'Volume control goes here
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…