Skip to content

Instantly share code, notes, and snippets.

@kishandonga
Last active June 26, 2020 04:44
Show Gist options
  • Save kishandonga/773dd438943381fa094cef69f390945c to your computer and use it in GitHub Desktop.
Save kishandonga/773dd438943381fa094cef69f390945c to your computer and use it in GitHub Desktop.
Android Preventing Double Click On A Button
import android.os.Handler;
public class FrequentClickBlocker {
private boolean mIsBlockClick = true;
public void release() {
new Handler().postDelayed(() -> mIsBlockClick = true, 1000);
}
public void block() {
mIsBlockClick = false;
}
public boolean status() {
return mIsBlockClick;
}
}
@kishandonga
Copy link
Author

btnAttachFile.setOnClickListener(new View.OnClickListener() {
            FrequentClickBlocker blocker = new FrequentClickBlocker();

            @Override
            public void onClick(View v) {
                if (blocker.status()) {
                    blocker.block();
                    <------- Your Work Here ------>
                    blocker.release();
                }
            }
        });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment