In my application i have a button. After single and double clicking of the button will perform separate operation. How can i do that? Thanks
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.
OnClickListener() { @Override public void onClick(View v) { // do something } }, 1000)); button. setOnClickListener(new MultiClickGuard(v -> {...}, 1000)); button. setOnClickListener(new MultiClickGuard(v -> doSomething(), 1000));
Pressing the default mouse button twice quickly opens or executes a program or opens a file. For example, while in Microsoft Windows or most other operating systems double-clicking a program icon opens that program.
Well it is simple just override.
onClick method of OnClickListener
public abstract class DoubleClickListener implements View.OnClickListener {
private static final long DEFAULT_QUALIFICATION_SPAN = 200;
private boolean isSingleEvent;
private long doubleClickQualificationSpanInMillis;
private long timestampLastClick;
private Handler handler;
private Runnable runnable;
public DoubleClickListener() {
doubleClickQualificationSpanInMillis = DEFAULT_QUALIFICATION_SPAN;
timestampLastClick = 0;
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
if (isSingleEvent) {
onSingleClick();
}
}
};
}
@Override
public void onClick(View v) {
if((SystemClock.elapsedRealtime() - timestampLastClick) < doubleClickQualificationSpanInMillis) {
isSingleEvent = false;
handler.removeCallbacks(runnable);
onDoubleClick();
return;
}
isSingleEvent = true;
handler.postDelayed(runnable, DEFAULT_QUALIFICATION_SPAN);
timestampLastClick = SystemClock.elapsedRealtime();
}
public abstract void onDoubleClick();
public abstract void onSingleClick();
}
Usage
button.setOnClickListener(new DoubleClickListener() {
@Override
public void onDoubleClick() {
Log.i("onClick", "double");
}
@Override
public void onSingleClick() {
Log.i("onClick", "single");
}
});
You may need to create a delay variable which will differenciate between single click and double click.
See this code,
private static final long DOUBLE_PRESS_INTERVAL = 250; // in millis
private long lastPressTime;
private boolean mHasDoubleClicked = false;
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Get current time in nano seconds.
long pressTime = System.currentTimeMillis();
// If double click...
if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) {
Toast.makeText(getApplicationContext(), "Double Click Event", Toast.LENGTH_SHORT).show();
mHasDoubleClicked = true;
}
else { // If not double click....
mHasDoubleClicked = false;
Handler myHandler = new Handler() {
public void handleMessage(Message m) {
if (!mHasDoubleClicked) {
Toast.makeText(getApplicationContext(), "Single Click Event", Toast.LENGTH_SHORT).show();
}
}
};
Message m = new Message();
myHandler.sendMessageDelayed(m,DOUBLE_PRESS_INTERVAL);
}
// record the last time the menu button was pressed.
lastPressTime = pressTime;
return true;
}
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