Prevent Multiple Clicks .Click()

Android Preventing Double Click On A Button

Disable the button with setEnabled(false) until it is safe for the user to click it again.

How to avoid multiple button click at same time in android?

The standard way to avoid multiple clicks is to save the last clicked time and avoid the other button clicks within 1 second (or any time span).
Example:

// Make your activity class to implement View.OnClickListener
public class MenuPricipalScreen extends Activity implements View.OnClickListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
// setup listeners.
findViewById(R.id.imageView2).setOnClickListener(MenuPricipalScreen.this);
findViewById(R.id.imageView3).setOnClickListener(MenuPricipalScreen.this);
...
}

.
.
.

// variable to track event time
private long mLastClickTime = 0;

// View.OnClickListener.onClick method defination

@Override
public void onClick(View v) {
// Preventing multiple clicks, using threshold of 1 second
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();

// Handle button clicks
if (v == R.id.imageView2) {
// Do your stuff.
} else if (v == R.id.imageView3) {
// Do your stuff.
}
...
}

.
.
.

}

Is there more effective way to prevent multiple click event in Android

You made it 90% there. You can abstract that last click timer into its own class that wraps an actual onClickListner. And when you implement it you override the wrapper and then you wont have to keep making the same logic over and over again. Here is a example in Java but should be easily convertible.

How to prevent rapid double click on a button

How to disable a button for multiple click?

I have made public method for avoid double clicking issue on view.

Please check this method,

/***
* To prevent from double clicking the row item and so prevents overlapping fragment.
* **/
public static void avoidDoubleClicks(final View view) {
final long DELAY_IN_MS = 900;
if (!view.isClickable()) {
return;
}
view.setClickable(false);
view.postDelayed(new Runnable() {
@Override
public void run() {
view.setClickable(true);
}
}, DELAY_IN_MS);
}

You can use the method by following way,

  buttonTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(String clickedText) {
Utils.avoidDoubleClicks(alertViewHolder.tv_alert);

// Rest code here for onlick listener

});

Or another way is,

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();
}
}

How to prevent rapid double click on a button

I am doing like this it works very well.

public abstract class OnOneOffClickListener implements View.OnClickListener {

private static final long MIN_CLICK_INTERVAL=600;

private long mLastClickTime;

public static boolean isViewClicked = false;


public abstract void onSingleClick(View v);

@Override
public final void onClick(View v) {
long currentClickTime=SystemClock.uptimeMillis();
long elapsedTime=currentClickTime-mLastClickTime;

mLastClickTime=currentClickTime;

if(elapsedTime<=MIN_CLICK_INTERVAL)
return;
if(!isViewClicked){
isViewClicked = true;
startTimer();
} else {
return;
}
onSingleClick(v);
}
/**
* This method delays simultaneous touch events of multiple views.
*/
private void startTimer() {
Handler handler = new Handler();

handler.postDelayed(new Runnable() {

@Override
public void run() {
isViewClicked = false;
}
}, 600);

}

}

Prevent multiple button click when one is clicked

Since we are looping through complaints we need a unique identifier to associate each button & form pair

 <!-- Edit icon -->
<div class="text-center">
<button class="py" type="button" data-id="{{ complaint.id }}" data-select="form-button">
<ion-icon size="large" name="create"></ion-icon>
</button>
</div>

<form data-id="{{ complaint.id }}" data-select="form" style="display:none"; method="POST" class="p-3" id="py1">
<!-- form elements ... ... ... -->
</form>

I am using data attribute for query selection you can even use class for that

JQuery code

<script>
$(document).ready(function () {
$('button[data-select="form-button"]').each(function () {
let buttonId = $(this).data('id')
$(this).bind('click', function () {
$(`form[data-select="form"][data-id="${buttonId}"]`).toggle();
});
});
});
</script>

jQuery preventing click event to be executed in case of double or multiple clicks by user

I found the answer for this.

$(document).on('click', '#clickable', function () {
$(this).prop('disabled', true);
//LOGIC
setTimeout(function () { $(this).prop('disabled', false); }, 500);
});

Its working for me. The set timeout for 500ms doesn't allow code to be re-entrant which is working fine for me at various network/device speeds.

How to prevent a double-click using jQuery?

jQuery's one() will fire the attached event handler once for each element bound, and then remove the event handler.

If for some reason that doesn't fulfill the requirements, one could also disable the button entirely after it has been clicked.

$(document).ready(function () {
$("#submit").one('click', function (event) {
event.preventDefault();
//do something
$(this).prop('disabled', true);
});
});

It should be noted that using the ID submit can cause issues, as it overwrites the form.submit function with a reference to that element.



Related Topics



Leave a reply



Submit