Cagri Yalcin
Cagri Yalcin

Reputation: 402

Android using two countdowntimers

In my app, i'm using two different CountDownTimers that have same values. I have two buttons to control them but when i press the button twice, it starting from the beginning. I want to keep its last value.

Here is my code:

t1 = new CountDownTimer(white, 1000) {
    @Override
    public void onTick(long l) {
        btnWhite.setText("seconds remaining: " + l / 1000);
        white = l;
    }

    @Override
    public void onFinish() {

    }
};

t2 = new CountDownTimer(black, 1000) {
    @Override
    public void onTick(long l) {
        btnBlack.setText("seconds remaining: " + l / 1000);
        black = l;
    }

    @Override
    public void onFinish() {

    }
};

btnBlack.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        t1.start();
        t2.cancel();
    }
});

btnWhite.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        t2.start();
        t1.cancel();
    }
});

Upvotes: 3

Views: 86

Answers (2)

user41805
user41805

Reputation: 533

I have tested this and it works!

I have two TextViews and two Buttons. The black button is next to the black text view and the white button is next to the white text view.

First I declare the important constants.

//contains the elapsed time for each of the timers
long blackElapsed=0,whiteElapsed=0;

//contains the total time with which we start new timers
long totalWhite = 30000;
long totalBlack = 30000;

Next I initialise the CountDownTimers. Whatever you put in here doesn't matter. I only have this so that the timers will be initialised with some value. The reason is that they have to be initialised in order to be able to .cancel() them later in the OnClickListeners.

black = new CountDownTimer(totalWhite, 1000){
    @Override
    public void onTick(long l) {
    }
    @Override
    public void onFinish() {
    }
};
white = new CountDownTimer(totalBlack, 1000){
    @Override
    public void onTick(long l) {
    }
    @Override
    public void onFinish() {
    }
};

Finally the OnClickListeners for the buttons. (W is white textView and B is black textView and b is black button and w is white button)

w.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        black.cancel();
        //using the elapsed time to start a new timer
        totalBlack = totalBlack - blackElapsed;
        //this preserves milliseconds by ticking every millisecond
        white = new CountDownTimer(totalBlack, 1){
            @Override
            public void onTick(long l) {
                B.setText(l+"");
                blackElapsed=totalBlack-l;  //updating the elapsed time
            }
            @Override
            public void onFinish() {
            }
        }.start();
    }
});
//we do a similar thing with the other player's button
b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        white.cancel();
        totalWhite = totalWhite - whiteElapsed;
        black = new CountDownTimer(totalWhite, 1){
            @Override
            public void onTick(long l) {
                W.setText(l+"");
                whiteElapsed=totalWhite-l;
            }
            @Override
            public void onFinish() {
            }
        }.start();
    }
});

Upvotes: 1

V-rund Puro-hit
V-rund Puro-hit

Reputation: 5534

I have checked your code.

It is obvious because your timers initialised with default values. when you start again it won't take new values of white/black.

To achieve what you want you have to initialise timer with new values before starting it.

I have done some correction in your code. you can check that out.

Make Two methods

public void timerStart1(long timeLengthMilli) {
    t1 = new CountDownTimer(timeLengthMilli, 1000) {
        @Override
        public void onTick(long l) {
            isRunning1 = true;
            tv1.setText("seconds remaining: " + l / 1000);
            white = l;
        }

        @Override
        public void onFinish() {
            isRunning1 = false;
        }
    }.start();
}

public void timerStart2(long timeLengthMilli) {
    t2 = new CountDownTimer(timeLengthMilli, 1000) {

        @Override
        public void onTick(long l) {
            isRunning2 = true;
            tv2.setText("seconds remaining: " + l / 1000);
            black = l;
        }

        @Override
        public void onFinish() {
            isRunning2 = false;
        }
    }.start();
}

and set setOnClickListener like this

button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (!isRunning1) {
            isRunning2 = false;
            timerStart1(white);
            if (t2 != null)
                t2.cancel();
        }
    }
});

button2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        if (!isRunning2) {
            isRunning1 = false;
            timerStart2(black);
            if (t1 != null)
                t1.cancel();
        }
    }
});

UPDATE :

Please check updated code and take these extra variables

boolean isRunning1 = false, isRunning2 = false;

Hope this will help you.

Happy Coding.

Upvotes: 1

Related Questions