I have looked at the answers here - Android Preventing Double Click On A Button
and implemented qezt\'s solution like and I\'ve tried setEnabled(false)
like so
Suru's answer worked well for me, thanks!
I'd like to add the following answer for anybody who's using https://github.com/balysv/material-ripple/blob/master/library/src/main/java/com/balysv/materialripple/MaterialRippleLayout.java and is facing this problem -
app:mrl_rippleDelayClick
is true by default.
This means, onClick will be executed only after it's finished showing the ripple. Hence setEnabled(false)
inside onClick will be executed after a delay because the ripple isn't done executing and in that period you may double click the view.
Set app:mrl_rippleDelayClick="false"
to fix this. This means the call to onClick will happen as soon as the view is clicked, instead of waiting for the ripple to finish showing.
try to set yourbutton.setClickable(false) like this :
onclick(){
yourbutton.setClickable(false) ;
ButtonLogic();
}
ButtonLogic(){
//your button logic
yourbutton.setClickable(true);
}
public static void disablefor1sec(final View v) {
try {
v.setEnabled(false);
v.setAlpha((float) 0.5);
v.postDelayed(new Runnable() {
@Override
public void run() {
try {
v.setEnabled(true);
v.setAlpha((float) 1.0);
} catch (Exception e) {
Log.d("disablefor1sec", " Exception while un hiding the view : " + e.getMessage());
}
}
}, 1000);
} catch (Exception e) {
Log.d("disablefor1sec", " Exception while hiding the view : " + e.getMessage());
}
}
I kept above method in a static file and i will call this method for all the buttonclick like this
button_or_textview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StaticFile.disablefor1sec(button_or_textview);
// Do Somthing.
}
});
I use a function like this in the listener of a button:
public static long lastClickTime = 0;
public static final long DOUBLE_CLICK_TIME_DELTA = 500;
public static boolean isDoubleClick(){
long clickTime = System.currentTimeMillis();
if(clickTime - lastClickTime < DOUBLE_CLICK_TIME_DELTA){
lastClickTime = clickTime;
return true;
}
lastClickTime = clickTime;
return false;
}
private static final long MIN_CLICK_INTERVAL = 1000;
private boolean isViewClicked = false;
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
final public void onClick(View view) {
if (isViewClicked) {
return:
}
// place your onClick logic here
isViewClicked = true;
view.postDelayed(new Runnable() {
@Override
public void run() {
isViewClicked = false;
}
}, MIN_CLICK_INTERVAL);
}
}