0

Here is my problem: rarely when I click on a button in my application "my screen" detects 2 or more clicks when I want to click only once. A kind of rebound. This causes my application to malfunction. I wanted to know if it is possible to generally redefine the click in my application.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
mmmhh
  • 11
  • 3
  • you can use a boolean to check if the button is clicked ,if it is clicked, set the boolean to false. Always check the value of the boolean before performing onbuttonclick function.Only perform function if the boolean is true – Usman Zafer Mar 26 '20 at 08:57
  • 1
    Thanks for the advice I thought about it, but I want something more general to the application. I was thinking of something like: ignore all clicks that occur after x milliseconds of a first click – mmmhh Mar 26 '20 at 09:06
  • Then if you are not hesistant to use rxjava,throttle from rxjava would be the approriate thing to apply in your case – Usman Zafer Mar 26 '20 at 09:28
  • I use rxJava, how can this solve the problem? – mmmhh Mar 26 '20 at 09:35
  • look at the example in the answer – Usman Zafer Mar 26 '20 at 09:48
  • check this answer, maybe help https://stackoverflow.com/questions/5608720/android-preventing-double-click-on-a-button/51959405#51959405 – GianhTran Mar 26 '20 at 10:44

1 Answers1

0

Throttle operator in Rxjava will wait for given time before executing the onNext() method

This is very helpful in your scenario.By using throttle operator, you can delay the onclick method execution,thereby allowing you to avoid the hassle of dealing with multiple clicks.

Add following library in your build.gradle

implementation 'com.jakewharton.rxbinding3:rxbinding:3.1.0'

The following code is an example. After a 500 MILLISECONDS delay,the onclick will be executed. This way we can ignore the subsequent clicks

 RxView.clicks(button).observeOn(AndroidSchedulers.mainThread())
                    .throttleFirst(500, TimeUnit.MILLISECONDS) // Throttle the clicks so 500 ms must pass before registering a new click
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Unit>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                           // disposables.add(d);
                        }
                        @Override
                        public void onNext(Unit unit) {
                            Log.d(TAG, "onNext: time since last clicked: " + (System.currentTimeMillis() - timeSinceLastRequest));
                          //  someMethod(); // Execute some method when a click is registered
                        }
                        @Override
                        public void onError(Throwable e) {
                        }
                        @Override
                        public void onComplete() {
                        }
                    });
Usman Zafer
  • 1,261
  • 1
  • 10
  • 15