Skip to content

Instantly share code, notes, and snippets.

@raquibulbari
Last active March 7, 2018 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raquibulbari/6051488 to your computer and use it in GitHub Desktop.
Save raquibulbari/6051488 to your computer and use it in GitHub Desktop.
package couk.doridori.android.lib.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
/**
* Class to work around the annoying queuing of multiple taps for Buttons. Could have a time attribute.
*
* This is useful as sometimes (on some platform / device combos) you can quickly tap on a button and end up
* starting an activity more than one. The reuests seem to be queued before the action has taken place so
* solutions like disabling the buttons or setting singleTop do not seem to work :(
*
* Taken from: https://github.com/doridori/AndroidUtilDump/blob/master/android/AndroidUtilDump/src/couk/doridori/android/lib/view/SafeButton.java
*
* @author dori
*/
public class SafeButton extends Button {
public SafeButton(Context context) {
super(context);
}
public SafeButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SafeButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setOnClickListener(OnClickListener l) {
super.setOnClickListener(new OnClickListenerWrapper(l));
}
private class OnClickListenerWrapper implements OnClickListener{
private static final long DEFAULT_MIN_INTERVAL = 1500;
private long mLastClickTime = 0;
private final OnClickListener mListener;
public OnClickListenerWrapper(OnClickListener listener){
mListener = listener;
}
@Override
public void onClick(View v) {
long currentTime = AnimationUtils.currentAnimationTimeMillis();
if(currentTime - mLastClickTime > DEFAULT_MIN_INTERVAL){
mListener.onClick(v);
mLastClickTime = currentTime;
}
}
}
}
@iruvingu
Copy link

iruvingu commented Mar 7, 2018

Do you have an example of how to make an instance of this class?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment