user1692261
user1692261

Reputation: 1217

Prevent android button from being pressed many times

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?

Upvotes: 0

Views: 832

Answers (2)

Catherine
Catherine

Reputation: 14000

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.

Upvotes: 4

Kirill Kulakov
Kirill Kulakov

Reputation: 10245

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...
}

Upvotes: 1

Related Questions