CountDown Timer App Android - java

I am creating a count down timer on the android platform.
I don't know how to store the remaining time on the timer (5 minutes on the timer) when the user presses the 'Stop' Button(PAUSE). So, if the user later presses the 'Start' Button again, it will continue counting down from where it left off. This is what i have done so far.
public class Timer_App extends Activity {
Button btn_Start, btn_Stop, btn_Reset;
TextView timer_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer_app);
//NEVER SLEEP!
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//Buttons
btn_Start = (Button) findViewById(R.id.Start);
btn_Stop = (Button) findViewById(R.id.Stop);
btn_Reset = (Button) findViewById(R.id.Reset);
//FONT
Typeface typeface = Typeface.createFromAsset(getAssets(), "Digital_Font.TTF");
timer_text = (TextView) findViewById(R.id.timer_view);
timer_text.setTypeface(typeface);
//Timer
timer_text.setText("05:00);
final CounterClass timer = new CounterClass(300000, 1000);
//Start btn
btn_Start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.start();
}
});
//Stop btn
btn_Stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
}
});
//Reset btn
btn_Reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
//TIMER CLASS
public class CounterClass extends CountDownTimer{
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
String hms = String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
timer_text.setText(hms);
}
#Override
public void onFinish() {
}
}
}

Related

How can i reset the timer if my button is clicked?

I have a textview and i made a timer to decrease from 45 seconds to 0
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_password);
TextView textView = (TextView) findViewById(R.id.textView6);
t1 = (TextView)findViewById(R.id.t1);
b1 = (Button) findViewById(R.id.button1);
new CountDownTimer(45000, 1000) {
public void onTick(long millisUntilFinished) {
t1.setText("00:" + millisUntilFinished / 1000);
if(millisUntilFinished<=10000){
t1.setText("00:0" + millisUntilFinished / 1000);
}
}
public void onFinish() {
t1.setText("Tempo acabado");
b1.setEnabled(true);
}
}.start();
I want after the time has expired give the option for the user to click the b1 button and restart the timer
You can create instance of CountDownTimer, then when you need to restart you can perform cancel and start on that instance.
CountDownTimer countDownTimer = new CountDownTimer(45000, 1000) {
//......................
}
//Start at first
countDownTimer.start();
//Restart when button is pressed
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
countDownTimer.cancel();
countDownTimer.start();
}
});
Is this what you want?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forgot_password);
TextView textView = (TextView) findViewById(R.id.textView6);
t1 = (TextView) findViewById(R.id.t1);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(view -> restartTimer());
restartTimer();
}
private void restartTimer() {
b1.setEnabled(false);
new CountDownTimer(45000, 1000) {
public void onTick(long millisUntilFinished) {
t1.setText("00:" + millisUntilFinished / 1000);
if (millisUntilFinished <= 10000) {
t1.setText("00:0" + millisUntilFinished / 1000);
}
}
public void onFinish() {
t1.setText("Tempo acabado");
b1.setEnabled(true);
}
}.start();
}

reset progressbar to 0 on backpressed

I am trying to use progressbar like a 30 secs timer. It works well but when i click back button the progressbar progress continues. I am unable to reset the progressbar progress to 0 when i click the backbutton.
Here is my code:
MainAcitivty:
public class MainActivity extends AppCompatActivity {
public Button btnA;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnA = (Button)findViewById(R.id.btnA);
btnA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentA = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intentA);
}
});
}
}
SecondActivity:
public class SecondActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Context context;
float from;
float to;
final TextView textV;
final ProgressBar mProgressBar;
CountDownTimer mCountDownTimer;
final int[] i = {0};
textV = (TextView)findViewById(R.id.tvFinal);
mProgressBar = (ProgressBar)findViewById(R.id.progressbar);
mProgressBar.setProgress(i[0]);
mCountDownTimer=new CountDownTimer(30000,1000) {
#Override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress"+ i[0] + millisUntilFinished);
i[0]++;
mProgressBar.setProgress((int) i[0] *100/(30000/1000));
}
#Override
public void onFinish() {
i[0]++;
mProgressBar.setProgress(100);
textV.setText("finish");
Intent intent = new Intent(SecondActivity.this, SecondActivity.class);
startActivity(intent);
}
};
mCountDownTimer.start();
}
}
in order to implement behaviour of back button you need to override onBackPressed() and then implement method to reset progressbar result.
Example:
#Override
public void onBackPressed() {
super.onBackPressed();
progressBar.setProgress(0);
}

How do I access my CountDownTimer to cancel it in Java?

Making a small game in Android Studio. Basically, the user will have a set amount of time to trigger a button press or the game will end. My CountDownTimer object is inside of a different function than my button click handler. How can I cancel the countDownTimer using cancel() from the button click handler.
Here is my code:
public countDownTimer timeLimit;
public void generate() {
final ProgressBar timer = (ProgressBar)findViewById(R.id.timer);`
int timeoutSeconds = 5000;
timer.setMax(timeoutSeconds);
timeLimit = new CountDownTimer(timeoutSeconds, 100) {
public void onTick(long millisUntilFinished) {
int timeUntilFinished = (int) millisUntilFinished;
timer.setProgress(timeUntilFinished);
}
public void onFinish() {
gameOver();
}
};
timeLimit.start();
}
public void buttonClicked(View v) {
timeLimit.cancel();
}
I'd be happy to hear any alternative ways to do this as well.
The code below works perfectly for me.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ProgressBar progressBar;
private CountDownTimer countDownTimer;
private Button stopTimerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar)findViewById(R.id.progressBar);
stopTimerButton = (Button)findViewById(R.id.button);
stopTimerButton.setOnClickListener(this);
int timeoutSeconds = 5000;
progressBar.setMax(timeoutSeconds);
countDownTimer = new CountDownTimer(timeoutSeconds,100) {
#Override
public void onTick(long millisUntilFinished) {
int timeUntilFinished = (int) millisUntilFinished;
progressBar.setProgress(timeUntilFinished);
}
#Override
public void onFinish() {
}
};
countDownTimer.start();
}
#Override
public void onClick(View view) {
if(view == stopTimerButton){
countDownTimer.cancel();
}
}
}

want to perform something untill button is pressed Android

In android,
for example
i want to perform a text.settext("something dynamic") until the buTTon is pressed. how to do it?
Try this:
android.os.Handler mHandler = new Handler();
Runnable rUpdateTextView = new Runnable() {
#Override
public void run () {
yourTextView.setText(returndate());
// Update your TextView every 200ms
mHandler.postDelayed(this, 200);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
[...]
mHandler.post(rUpdateTextView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mHandler.removeCallbacks(rUpdateTextView);
}
});
}
//click event for your button
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String time = returndate();
//set your text to textView
test.setText(time);
}
});
}

TextView not Displaying Timer

For some reason whenever I try to update my TextView to show how many seconds a person has left. But for some reason now it isn't working.
public class MainActivity extends Activity {
private int index=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
final int[] Times= {6000,3000,7000,3000,4000,6000,3000};
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
final TextView textview =(TextView) findViewById(com.example.countdowntest.R.id.textView);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if(index<Times.length){
CountDownTimer timer = new CountDownTimer(
Times[index], 0) {
public void onTick(long millisUntilFinished) {
textview.setText("seconds remaining: "+ millisUntilFinished / 1000);
}
public void onFinish() {
textview.setText("done!");
index++;
}
}.start();
}
}
});
}
}
It should display the updated seconds until it is finished because it is all within the onClick method which doesn't end until the timer reaches zero.
Your countDownInterval is 0
CountDownTimer(long millisInFuture, long countDownInterval)
What you have
new CountDownTimer(Times[index], 0) { // second param to the constructor is 0.
Should be
new CountDownTimer(30000, 1000) // whatever value you like
http://developer.android.com/reference/android/os/CountDownTimer.html
It won't update because you are passing 0 try passing 1000 which will update it at 1 second interval

Categories