alanmahony
Lurker
Hey Guys,
Only new to Android development and have been working on first app for 2 days.
Have gotten mapping and feedback forms working but stuck on a countdown timer.
I have written code for one to countdown from a set number of days and hours and minutes but once the app closes the timer countdown from the set days hours minutes again.
What i am looking to do is have a specific day in July and a specific time set and when app opens it checks the time on the phone and then displays in a TextView the days hours minutes sand seconds from the current day which say is today to the date and time in July.
Sorry if this has been asked before but i could not find in a search of the forums.
CountDownTimer mCountDownTimer;
long mInitialTime = DateUtils.DAY_IN_MILLIS * 2 +
DateUtils.HOUR_IN_MILLIS * 9 +
DateUtils.MINUTE_IN_MILLIS * 3 +
DateUtils.SECOND_IN_MILLIS * 42;
TextView txtTimer;
txtTimer = (TextView) findViewById(R.id.txtTimer);
mCountDownTimer = new CountDownTimer(mInitialTime, 1000) {
StringBuilder time = new StringBuilder();
@override
public void onFinish() {
txtTimer.setText(DateUtils.formatElapsedTime(0));
txtTimer.setText("Wedding Time");
}
@override
public void onTick(long millisUntilFinished) {
time.setLength(0);
if(millisUntilFinished > DateUtils.DAY_IN_MILLIS) {
long count = millisUntilFinished / DateUtils.DAY_IN_MILLIS;
if(count > 1)
time.append(count).append(" days ");
else
time.append(count).append(" day ");
millisUntilFinished %= DateUtils.DAY_IN_MILLIS;
}
time.append(DateUtils.formatElapsedTime(Math.round(millisUntilFinished / 1000d)));
txtTimer.setText(time.toString());
}
}.start();
This is what i'm using at the minute.
Thank You for any and all help.
Only new to Android development and have been working on first app for 2 days.
Have gotten mapping and feedback forms working but stuck on a countdown timer.
I have written code for one to countdown from a set number of days and hours and minutes but once the app closes the timer countdown from the set days hours minutes again.
What i am looking to do is have a specific day in July and a specific time set and when app opens it checks the time on the phone and then displays in a TextView the days hours minutes sand seconds from the current day which say is today to the date and time in July.
Sorry if this has been asked before but i could not find in a search of the forums.
CountDownTimer mCountDownTimer;
long mInitialTime = DateUtils.DAY_IN_MILLIS * 2 +
DateUtils.HOUR_IN_MILLIS * 9 +
DateUtils.MINUTE_IN_MILLIS * 3 +
DateUtils.SECOND_IN_MILLIS * 42;
TextView txtTimer;
txtTimer = (TextView) findViewById(R.id.txtTimer);
mCountDownTimer = new CountDownTimer(mInitialTime, 1000) {
StringBuilder time = new StringBuilder();
@override
public void onFinish() {
txtTimer.setText(DateUtils.formatElapsedTime(0));
txtTimer.setText("Wedding Time");
}
@override
public void onTick(long millisUntilFinished) {
time.setLength(0);
if(millisUntilFinished > DateUtils.DAY_IN_MILLIS) {
long count = millisUntilFinished / DateUtils.DAY_IN_MILLIS;
if(count > 1)
time.append(count).append(" days ");
else
time.append(count).append(" day ");
millisUntilFinished %= DateUtils.DAY_IN_MILLIS;
}
time.append(DateUtils.formatElapsedTime(Math.round(millisUntilFinished / 1000d)));
txtTimer.setText(time.toString());
}
}.start();
This is what i'm using at the minute.
Thank You for any and all help.