Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I start and stop my countdowntimer via a button?

This is what I have so far.. it just starts when the application is opened:

package com.android.countdown;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.TextView;

public class countdown extends Activity {

    TextView mTextField;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mTextField = (TextView) findViewById(R.id.timer1);

        new CountDownTimer(100000, 1000) {
            public void onTick(long millisUntilFinished) {
                mTextField.setText("Seconds remaining: " + millisUntilFinished / 1000);
            }

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

I know that I need to call start() inside a button procedure. However, if I move the .start() from where it's at the new CountDownTimer(100000 , 1000) { gets an error.

like image 462
MJ93 Avatar asked Jan 24 '11 22:01

MJ93


1 Answers

Well... maybe you need to first understand how Java and programming work. Then, you can try to do something like this:

CountDownTimer aCounter = new CountDownTimer(100000 , 1000) {
    public void onTick(long millisUntilFinished) {
        mTextField.setText("Seconds remaining: " + millisUntilFinished / 1000);
    }

    public void onFinish() {
        mTextField.setText("Finished");
    }
};
aCounter.start();
like image 186
Cristian Avatar answered Oct 02 '22 21:10

Cristian