4

I have a problem that i can't work out. I have a button that when clicked changes the text view. It then activates a postdelayed process that returns the textview to its original text after 2 seconds.

If i press the button once, and then again within this 2 second interval the postdelay will continue to count down from the first press and not restart itself from the second press. This results in the original text being shown when i want the changed text to be.

Each time the button is pressed it creates a delay from that instance. I want it to cancel the previous postdelay and start a new one. This is my code so far but its not finished because i can't work out how to finish it (so it does not work).

p1AddL.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            counter1 ++;
            count1 ++;
            Handler h = new Handler();
            if ('PREVIOUS_DELAY_HAS_STARTED') {
                h.removeCallbacks(clickButton);
                h.postDelayed(clickButton, 2000);
            } else {
                h.postDelayed(clickButton, 2000);
            }
            if (count1 > 0) {
                lifepointsP1.setText("+" + count1);
                lifepointsP1.setTextColor(Color.argb(220, 0, 188, 0));
            }
        }
    });

Runnable clickButton = new Runnable() {
    @Override
    public void run() {
        count1 = 0;
        lifepointsP1.setTextColor(Color.WHITE);
        lifepointsP1.setText(counter1);
    }
};

the PREVIOUS_DELAY_HAS_STARTED text needs to be some sort of checking method and i'm pretty sure i need something between the h.removeCallbacks and h.postDelayed commands under that text.

If their is a simpler way/better way to write this method to make it work please let me know. I have tried so many ways and i feel i am very close here.

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
RhysBailey21
  • 117
  • 3
  • 13

1 Answers1

7

removeCallbacks won't do anything if clickButton isn't registered on h. So you can simply replace

if ('PREVIOUS_DELAY_HAS_STARTED') {
    h.removeCallbacks(clickButton);
    h.postDelayed(clickButton, 2000);
} else {
    h.postDelayed(clickButton, 2000);
}

with

h.removeCallbacks(clickButton);
h.postDelayed(clickButton, 2000);
0101100101
  • 5,786
  • 6
  • 31
  • 55
  • thanks for your fast reply. although, this has not fixed the problem. I had it like this originally. It still causes a delay countdown to happen on every click and not to cancel the last click and start a new one. Would you guys suggest anything else that could work? – RhysBailey21 Aug 13 '14 at 11:07
  • 1
    ok, so maybe create a Handler once and set it to class field – pskink Aug 13 '14 at 11:10
  • Nice, that's the solution. You create a new Handler each time, which of course does not have anything posted to it. – 0101100101 Aug 13 '14 at 11:12
  • yes! excatly. Thank you. you both fixed the problem. i have been stuck on this for days! – RhysBailey21 Aug 13 '14 at 11:22