I have an android app in which count up timer is included I used a chronometer for it.....
Now there is a scenario that after every 10 sec, 1 sec should change in chronometer I used handler.postdelyed()
but in this, the chronometer starts running after 10 sec but the timer of chronometer starts from 10 sec, not 1 sec. Is there any method that I can make count up timer with delay in it.
Here is my code:
chronometer_life_gained.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(final Chronometer chronometer) {
long delay = (206 / Long.parseLong(joint_per_day))*1000;
System.out.println("delayer " + delay);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if ((SystemClock.elapsedRealtime() - chronometer.getBase()) < 60000) {
long dy = TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase());
final long hr = TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long min = TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(SystemClock.elapsedRealtime() - chronometer.getBase()));
final long sec = TimeUnit.MILLISECONDS.toSeconds(SystemClock.elapsedRealtime() - chronometer.getBase())
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(SystemClock.elapsedRealtime() - chronometer.getBase()));
year_num_life_gained.setVisibility(View.GONE);
year_text_life_gained.setVisibility(View.GONE);
month_num_life_gained.setVisibility(View.GONE);
month_text_life_gained.setVisibility(View.GONE);
day_num_life_gained.setVisibility(View.GONE);
day_text_life_gained.setVisibility(View.GONE);
hour_num_life_gained.setVisibility(View.GONE);
hour_text_life_gained.setVisibility(View.GONE);
min_num_life_gained.setVisibility(View.GONE);
min_text_life_gained.setVisibility(View.GONE);
sec_num_life_gained.setVisibility(View.VISIBLE);
sec_text_life_gained.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_VERTICAL);
params.addRule(RelativeLayout.ALIGN_PARENT_START);
sec_num_life_gained.setLayoutParams(params);
sec_num_life_gained.setText(Long.toString(sec));
}
//
}
}, delay);
// }
}
});
Thanks in advance
You can achieve it with CountDownTimer
, although it is by name count down timer, but we can make it work to count up.
// Counter variable to show seconds increment
int counter = 4000;
// If you want to count until 60 seconds, put VALUE_IN_MILLIS_UNTIL_YOU_WANT_TO_COUNT_UP = 60000 (in milliseconds)
elapsedTimer = new CountDownTimer(VALUE_IN_MILLIS_UNTIL_YOU_WANT_TO_COUNT_UP, 5000) {
@Override
public void onTick(long l) {
// Logically, we need
long elapsedTime = SystemClock.elapsedRealtime() - startTime - counter;
long dy = TimeUnit.MILLISECONDS.toDays(elapsedTime);
final long yr = dy / 365;
dy %= 365;
final long mn = dy / 30;
dy %= 30;
final long hr = TimeUnit.MILLISECONDS.toHours(elapsedTime)
- TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(elapsedTime));
final long min = TimeUnit.MILLISECONDS.toMinutes(elapsedTime)
- TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(elapsedTime));
final long sec = TimeUnit.MILLISECONDS.toSeconds(elapsedTime)
- TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(elapsedTime));
// add 4000 as 4 seconds to counter value
counter = counter + 4000;
txView.setText(counter + " second");
}
}.start();