Countdown timer in Android

package com.spec; import; import android.os.Bundle; import android.os.CountDownTimer; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class CountdwntimerActivity extends Activity implements onClickListener { private MyCountDownTimer myCountDownTimer; private boolean mTimerHasStarted = false; private Button mButton; private TextView mTextView; private final long mStartTime = 10 * 1000; private final long mInterval = 1 * 1000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton = (Button) this.findViewById(; mButton.setOnClickListener(this); mTextView = (TextView) this.findViewById(; myCountDownTimer = new MyCountDownTimer(mStartTime, mInterval); mTextView.setText(mTextView.getText() + String.valueOf(mStartTime)); } @Override public void onClick(View v) { if (!mTimerHasStarted) { myCountDownTimer.start(); mTimerHasStarted = true; mButton.setText("stop"); } else { myCountDownTimer.cancel(); mTimerHasStarted = false; mButton.setText("Start"); } } public class MyCountDownTimer extends CountDownTimer { public MyCountDownTimer(long startTime, long interval) { super(startTime, interval); } @Override public void onFinish() { mTextView.setText("Time's up!"); } @Override public void onTick(long millisUntilFinished) { long sec = millisUntilFinished/1000; mTextView.setText("Time remain:" + sec); } } }
Here is code snippet for countdown timer in android. It uses "CountDownTimer" class.

#timer #countdown #android


Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.