0

I tried Thread.sleep(x) and I tried

setEnabled(false); setClickable(false); 

But neither worked as expected. I read somewhere that the clicks are queued and therefore the previous solutions didn't worked.

Any ideas?

2 Answers 2

4

You could set a flag in your onClick, something like clickable = false once it's been clicked once, and then re-enable it when you want. Run the relevant onClick code only if clickable is true.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I used something like that (I used time check)
1

You could disable it once clicked. add in the xml a listner for the on click

<Button android:id="@+id/button1" android:layout_height = "wrap_content" android:layout_width ="wrap_content" android:text = "lets do this" android:onClick = "DoIt" /> 

And then disable it within the on click listner

public void DoIt(View v){ ((Button) v).setEnabled(false); ...your code... } 

4 Comments

This doesn't work. The clicks are queued. So if the user clicks 3 times quickly, there are 3 calls to the DoIt() method queued. Once the first call gets executed, the button will be disabled, but by then it is too late and the other 2 calls to DoIt() will still occur.
@DavidWasser sorry for late comment. But perhaps you didn't put the line of code in the right place.
@Cԃաԃ No. It makes no difference where you put that line of code. Disabling a button in the onClick() method is not reliable, because the Android framework queues the button click events in an event queue. onClick() methods are not called synchronously when the user clicks the button. Therefore, if the user is quick enough (or the device is slow enough), it is always possible to get multiple click events in the queue before the first onClick() method is called.
@DavidWasser, I was just curious about it as I use it in my apps. Thanks for explaining, I have not yet faced this issue myself, but it is always good to learn. I also used the old trick with -if-else-boolean combo, but I guess it has the same effect.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.