-1

On click on button call event or execute its corresponding method as many as times when pressed very fast?what is the best approach to handle this in android?

  • check this out...https://stackoverflow.com/questions/16534369/avoid-button-multiple-rapid-clicks – Cavan Page Jul 18 '17 at 16:28
  • This question is very difficult to understand. Explain what you did, explain what actually happened, and then explain what you wanted to happen. Use more words. It might be useful to ask a friend to help you write the question in clear English. – Buster Jul 18 '17 at 16:40
  • Actually its a form and when user press the submit button entry gets saved my app users are complaining of same entries punched from app and i have used the above approach in a simple way by taking a variable of time interval and if time difference between successive punches is less than 2 second i am simply returning but still i am facing the issue – himanshu Jul 18 '17 at 16:58

3 Answers3

0

on_tap is a common event I never had a problem using for this. You can "tap" as fast as you can, and the event should be raised every time.

doriclazar
  • 97
  • 7
0
onClick(View v) { 
MultiClickPreventer.preventMultiClick(v); 
//your op here 
} 

...

public class MultiClickPreventer { 
private static final long DELAY_IN_MS = 500;

public static void preventMultiClick(final View view) {
    if (!view.isClickable()) {
        return; 
    } 
    view.setClickable(false);
    view.postDelayed(new Runnable() {
        @Override 
        public void run() { 
            view.setClickable(true);
        } 
    }, DELAY_IN_MS);
} 

}

GAGAN BHATIA
  • 591
  • 5
  • 17
  • used this approach sir but didn't work for me the events get stored in queue before clickable is set to false – himanshu Jul 18 '17 at 16:52
0

As you've mentioned, it is possible for multiple button click events to be enqueued before the first click event is handled in onClick(), so simply calling setClickable(false) or setEnabled(false) won't always work.

However, even though you can have multiple events queued up, they will always be handled on the same thread, which means there will always be a first click event.

Store a boolean of your own and only perform your onClick() actions the first time.

private boolean firstEvent = true;

public void onClick(View v) {
    if (firstEvent) {
        firstEvent = false;
        // your code here
    }
}
Ben P.
  • 52,661
  • 6
  • 95
  • 123
  • but i have multiple buttons in my form and setting a boolean value for each of them is not a good practice. – himanshu Jul 18 '17 at 17:20
  • but i show alert dialogs and have around 3 to 4 buttons also do you think its a good idea to have separate variable set for all of them and setting them all back to true if there is any network connection issue while making network call on submitting ? – himanshu Jul 18 '17 at 17:39
  • If you need special behavior for your buttons, you're going to have to write code to create that special behavior. – Ben P. Jul 18 '17 at 18:00