Last active
October 16, 2023 11:43
-
-
Save srix55/ec64d2f6a371c80bbbc4 to your computer and use it in GitHub Desktop.
Simple double-click listener that can be used like the way OnClickListener is used on a view in Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.SystemClock; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
/** | |
* A simple double-click listener | |
* Usage: | |
* // Scenario 1: Setting double click listener for myView | |
* myView.setOnClickListener(new DoubleClickListener() { | |
* | |
* @Override | |
* public void onDoubleClick() { | |
* // double-click code that is executed if the user double-taps | |
* // within a span of 200ms (default). | |
* } | |
* }); | |
* | |
* // Scenario 2: Setting double click listener for myView, specifying a custom double-click span time | |
* myView.setOnClickListener(new DoubleClickListener(500) { | |
* | |
* @Override | |
* public void onDoubleClick() { | |
* // double-click code that is executed if the user double-taps | |
* // within a span of 500ms (default). | |
* } | |
* }); | |
* | |
* @author Srikanth Venkatesh | |
* @version 1.0 | |
* @since 2014-09-15 | |
*/ | |
public abstract class DoubleClickListener implements OnClickListener { | |
// The time in which the second tap should be done in order to qualify as | |
// a double click | |
private static final long DEFAULT_QUALIFICATION_SPAN = 200; | |
private long doubleClickQualificationSpanInMillis; | |
private long timestampLastClick; | |
public DoubleClickListener() { | |
doubleClickQualificationSpanInMillis = DEFAULT_QUALIFICATION_SPAN; | |
timestampLastClick = 0; | |
} | |
public DoubleClickListener(long doubleClickQualificationSpanInMillis) { | |
this.doubleClickQualificationSpanInMillis = doubleClickQualificationSpanInMillis; | |
timestampLastClick = 0; | |
} | |
@Override | |
public void onClick(View v) { | |
if((SystemClock.elapsedRealtime() - timestampLastClick) < doubleClickQualificationSpanInMillis) { | |
onDoubleClick(); | |
} | |
timestampLastClick = SystemClock.elapsedRealtime(); | |
} | |
public abstract void onDoubleClick(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, @Duna !