Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Count Down Timer e.g. 10:00 to 00:00? using OnclickListener to TextView?

Tags:

java

android

I'm trying to make a countdown timer starting at 10 minutes, similair to a basketball scoreboard: 10:00 to 00:00. How would I do that? This is my code:

private TextView Timer;  
Handler handler = new Handler();  
private int length = 120000;  
private int decision = 0;  
MyCount counter;  

public String formatTime(long millis) {  
    String output = "00:00";  
    long seconds = millis / 1000;  
    long minutes = seconds / 60;  

    seconds = seconds % 60;  
    minutes = minutes % 60;  

    String sec = String.valueOf(seconds);  
    String min = String.valueOf(minutes);  

    if (seconds < 10)  
        sec = "0" + seconds;  
    if (minutes < 10)  
        min= "0" + minutes;  

    output = min + " : " + sec;  
    return output;
}//formatTime  

@Override  
public void onCreate(Bundle cute) {  
    super.onCreate(cute);  
    counter = new MyCount(length, 1000);  
    updateTime();  
    handler.removeCallbacks(updateTimeTask);  
    handler.postDelayed(updateTimeTask, 1000);   
}//end of cuteness  

private Runnable updateTimeTask = new Runnable() {  
    public void run() {  
        updateTime();  
        handler.postDelayed(this, 1000);  
    }  
};  

private void updateTime() {  
    switch (decision) {  
        case 0:  
            startTime = 0L;  
            counter.start();  
            decision=1;  
            break;  
        case 1:  
            counter.onPause();  
            decision=0;  
            break;  
    }  
}//updateTime  

class MyCount extends CountDownTimer {  
    public MyCount(long millisInFuture, long countDownInterval) {  
        super(millisInFuture, countDownInterval);  
    }//MyCount  

    public void onPause() {  
        //do stuff later  
        onPause();  
    }//finish  

    public void onTick(long millisUntilFinished) {             
        Timer.setText("" + formatTime(millisUntilFinished));           
    }//on tick  

    @Override  
    public void onFinish() {  
        onStop();  

    }//finish  
}//class MyCount  

Any help would be appreciated. thanks!

like image 742
Mineko Avatar asked Nov 28 '11 07:11

Mineko


1 Answers

This does not have to be too hard. You've already created your functionality for writing your time in letters, now you need to count down. Starting a timer is easy, just do this in your start button event handler (or whatever you choose to use) (modified example from the android developer reference:

// New timer for 10 minutes, starts after initialization
new MyCount(600000, 1000) 
{
    // Updates the text on your "scoreboard" every second
    public void onTick(long millisUntilFinished) 
    {
        Timer.setText("Time remaining: " + formatTime(millisUntilFinished));
    }

    public void onFinish() 
    {
        mTextField.setText("done!");
    }
}.start();

And that's all you need! You can skip your UpdateTime function and your updateTimeTask. Just replace all this on your onCreate method

counter = new MyCount(length, 1000);  
updateTime();  
handler.removeCallbacks(updateTimeTask);  
handler.postDelayed(updateTimeTask, 1000); 

With my code. Or modify it as you please!

like image 72
Tobbe Avatar answered Oct 04 '22 02:10

Tobbe