Android Button edit

Custom Click Listener to prevent multiple fast clicks

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
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close