Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using countdown to set button enabled

I am trying to avoid repeated touches, but want a sensitive screen. Using the method advised in this answer:

Android Preventing Double Click On A Button

I have been re-enabling buttons within the program flow, but there are some instances where, I cannot rely on program flow and need to ensure the button is enabled.

I have devised a way to re-enable these button using a countdowntimer and shown in my code:

button.setOnTouchListener(new View.OnTouchListener() {
        @Override public boolean onTouch(View v, MotionEvent event) {
            disableButton(button);
            countDwn();
            // Do something
            return false;
        }
    });

public void disableButton(Button button) {
    button.setEnabled(false);
}

public void enableButton(Button button) {
    button.setEnabled(true);
}

public void countDwn() {
    CountDownTimer countDownTimer = new CountDownTimer(2000, 1000) {
        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() {
            enableButton(button);
        }
    }.start();
}

What I am concerned about, is that this may be a clumsy or ineffective way to go about this. I am wanting advice about this and if anyone has a more elegant suggestion?


1 Answers

I am not sure setting a button to enable=false is the correct way to solve the "repeated touches" issue.

The main reason for that is since when a button is enable=false, most of the time it will have a Disabled graphics assigned to it. Since we only want to prevent accidental repeated touches, and not to invoke the disable graphics, I am not sure this is the correct solution.

Prevent Repeated Touches

I will suggest a simpler solution, prevent the action if the time from last action is less than MINIMUM_ACTION_DELAY.

If you want to get the click animation, prevent the action on the onClick listener. If you don't want the click animation, prevent the action on the onTouch.

For example, onClick will be something like this:

button.setOnClickListener(new View.OnClickListener() {

    private long mLastActionTime;

    @Override
    public void onClick(View v) {
        long currentTime = System.currentTimeMillis();
        if (currentTime - mLastActionTime < MINIMUM_ACTION_DELAY) {
            // Too soon, we don't want to handle this event
            return;
        }

        // Save the action time
        mLastActionTime = currentTime;

        // Execute action
        // TODO do something
    }
});
like image 197
Eyal Biran Avatar answered Jun 01 '26 17:06

Eyal Biran