How to resolve double tap on Button Issue in android?
This is called debouncing- its a classical problem in hardware and in software. There's a couple of tricks you can do, but they all boil down to disabling the button temporarily and re-enabling it later. This can be done via timer (get the time they click on it, save it, and if they click it again within say 100ms ignore the 2nd click). Another way would be to disable the button after onClick and re-enable it when the new Activity finishes via onActivityResult. Or there's a dozen other ways, pick the easiest for you.
You can set launchMode of ActivitySecond
to singleTop
<activity android:name=".ActivitySecond"
android:launchMode="singleTop"
>
...
</activity>
btn.setOnclickListener(new View.onClickListener(){
public void onClick(View v) {
btn.setEnabled(false);
}
});
you have to make the setEnabled(false) in onlclick event.
As Gabe Sechan sad:
This can be done via timer (get the time they click on it, save it, and if they click it again within say 100ms ignore the 2nd click)
Here is an implementation that i used in my project:
public abstract class OnOneClickListener implements View.OnClickListener {
private static final long MIN_CLICK_INTERVAL = 1000; //in millis
private long lastClickTime = 0;
@Override
public final void onClick(View v) {
long currentTime = SystemClock.elapsedRealtime();
if (currentTime - lastClickTime > MIN_CLICK_INTERVAL) {
lastClickTime = currentTime;
onOneClick(v);
}
}
public abstract void onOneClick(View v);
}
Just use OnOneClickListener
instead of OnClickListener
and execute your code in onOneClick()
method.
The solution with disabling button in onClick()
will not work. Two clicks on a button can be scheduled for execution even before your first onClick()
will execute and disable the button.