I want to implement repeat action on pressing and holding a button. Example: When user click on a button and hold it,it should call a similar method again and again on a fixed interval until the user remove his finger from the button.
I want to implement repeat action on pressing and holding a button. Example: When
Share
There are multiple ways to accomplish this, but a pretty straightforward one would be to post a
Runnableon aHandlerwith a certain delay. In it’s most basic form, it will look somewhat like this:The idea is pretty simple: post a
Runnablecontaining the repeated action on aHandlerwhen the ‘down’ touch action occurs. After that, don’t post theRunnableagain until the ‘up’ touch action has passed. TheRunnablewill keep posting itself to theHandler(while the ‘down’ touch action is still happening), until it gets removed by the touch up action – that’s what enables the ‘repeating’ aspect.Depending on the actual behaviour of the button and its onclick/ontouch you’re after, you might want to do the initial post without a delay.