Custom Click Listener to prevent multiple fast clicks

suggest change

In order to prevent a button from firing multiple times within a short period of time (let’s say 2 clicks within 1 second, which may cause serious problems if the flow is not controlled), one can implement a custom SingleClickListener.

This ClickListener sets a specific time interval as threshold (for instance, 1000ms). When the button is clicked, a check will be ran to see if the trigger was executed in the past amount of time you defined, and if not it will trigger it.

public class SingleClickListener implements View.OnClickListener {

    protected int defaultInterval;
    private long lastTimeClicked = 0;

    public SingleClickListener() {

    public SingleClickListener(int minInterval) {
        this.defaultInterval = minInterval;

    public void onClick(View v) {
        if (SystemClock.elapsedRealtime() - lastTimeClicked < defaultInterval) {
        lastTimeClicked = SystemClock.elapsedRealtime();

    public abstract void performClick(View v);


And in the class, the SingleClickListener is associated to the Button at stake

myButton = (Button) findViewById(;
myButton.setOnClickListener(new SingleClickListener() {
    public void performClick(View view) {
        // do stuff

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents