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


Some times user may clicks multiple times on same widget, at that time App misleads and gives crassh/un expected behaviour to user.
We can resolve this by using the below code

The standard way to avoid multiple clicks is to save the last clicked time and avoid the other widget clicks within a preferred time

 

private long lastPressesTime;

@Override
public void onClick(View v) {
    // Preventing multiple clicks, using threshold of 1 second
    if (SystemClock.elapsedRealtime() - lastPressesTime < 1000) {
        return;
          }
    lastPressesTime = SystemClock.elapsedRealtime();
    switch(v.getId())
    {
        case R.id.widget1:
        break;
        case R.id.widget2:
        break;
    }
          
      
 }

 


Flutter Questions

Android Questions

Python Questions

Java Questions