Prevent double click on Android buttons
Sometimes user clicks button too fast or double time, if button performs some kind of network operation, it'll call the function multiple times. To prevent double click, you can record the last time button clicked, and compare it to threshold of desired time.
private long lastClickTime = 0;
View.OnClickListener buttonHandler = new View.OnClickListener() {
public void onClick(View v) {
// preventing double, using threshold of 1000 ms
if (SystemClock.elapsedRealtime() - lastClickTime < 1000){
return;
}
lastClickTime = SystemClock.elapsedRealtime();
}
}
Written by Sardor Isakov
Related protips
2 Responses
To prevent this, you can use ProgressDialog with attribute cancelable(false)
over 1 year ago
·
NO. ProgressDialog is not perfect enough. Android queue up the click event. So two ProgressDialogs may show up when click fast enough.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Java
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#