Mockup of my Application :
Problem :
When click on button1 it just call Intent of ActivitySecond
button1.setOnClickListener(this);
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.button1:
Intent intent = new Intent(getApplicationContext(), ActivitySecond.class);
startActivity(intent);
break;
default:
break;
}
}
But, on Double tap it open twice ActivitySecond.
HOW TO RESOLVE IT.
PLEASE IF ANY SOLUTION THEN SHARE IT.
Thank you.
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.
To switch the behavior of a single click between left click and right click, tap the Mouse button. A double tap acts as a double click. A double tap and hold allows you to grab and then drag. A two-finger tap acts as a right click.
1. Double-click is a term used to describe the process of quickly pressing a mouse button twice while keeping it still. In most cases, a double-click is with the left mouse button and is used to open or execute a file, folder, or software program.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With