Reputation: 117
I have developed a CountDown Timer with ProgressBar it working fine but I am not sure how to pause and resume the timer when I click fab_play_pause button. please i need help, thank you so much
This is my code:
public class StandingSideStretch extends Fragment {
View rootView;
private TextView timer;
private FloatingActionButton fab_play_pause, fab_next, fab_back;
private ProgressBar mProgress;
MyCountDownTimer myCountDownTimer;
private CountDownTimer countDownTimer;
private final long startTime = 60 * 1000;
private final long interval = 1 * 1000;
private boolean timerHasStarted = false;
private ObjectAnimator animation;
private static final String PLAY_ICON = "playIcon";
private static final String PAUSE_ICON = "pauseImage";
public StandingSideStretch() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_standing_side_stretch, container, false);
setFabs();
timer = (TextView)rootView.findViewById(R.id.timer);
countDownTimer = new MyCountDownTimer(startTime, interval);
timer.setText("60's");
return rootView;
}
private void setFabs(){
fab_play_pause = (FloatingActionButton)rootView.findViewById(R.id.fab_play_pause);
fab_back = (FloatingActionButton) rootView.findViewById(R.id.fab_back);
fab_next = (FloatingActionButton) rootView.findViewById(R.id.fab_next);
fab_play_pause.setImageResource(R.drawable.ic_play_arrow_white_24dp);
fab_play_pause.setTag(PLAY_ICON);
fab_play_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String tag = (String) view.getTag();
if (view.getTag() == PLAY_ICON){
fab_play_pause.setTag(PAUSE_ICON);
fab_play_pause.setImageResource(R.drawable.ic_pause_white_24dp);
timerHasStarted = true;
countDownTimer.start();
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.custom_progressbar_drawable);
final ProgressBar mProgress = (ProgressBar) rootView.findViewById(R.id.circularProgressbar);
mProgress.setProgress(0); // Main Progress
mProgress.setSecondaryProgress(100); // Secondary Progress
mProgress.setMax(100); // Maximum Progress
mProgress.setProgressDrawable(drawable);
mProgress.setRotation(0);
animation = ObjectAnimator.ofInt(mProgress, "progress", 0, 100);
animation.setDuration(startTime);
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
}else if (view.getTag() == PAUSE_ICON){
fab_play_pause.setTag(PLAY_ICON);
fab_play_pause.setImageResource(R.drawable.ic_play_arrow_white_24dp);
countDownTimer.cancel();
animation.cancel();
}
}
});
fab_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
public class MyCountDownTimer extends CountDownTimer{
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onTick(long millisUntilFinished) {
timer.setText("" + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
timer.setText("0");
}
}}
Upvotes: 0
Views: 1067
Reputation: 1409
I was writing an app which allowed a runner to enter a distance, a target time and their stride length, and using these would calculate the interval at which to sound a pace tone. I had a start/stop button to allow the runner to toggle the tone on/off. The only way I could get this to work was to cancel the timer activity and then create a new timer. Here's the code which worked for me.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if ( ! running ) {
running = true;
button.setText("STOP");
float distance = Float.parseFloat((String) ((TextView) findViewById(R.id.editTextDistance)).getText().toString());
float duration = Float.parseFloat((String) ((TextView) findViewById(R.id.editTextDuration)).getText().toString());
float stepSize = Float.parseFloat((String) ((TextView) findViewById(R.id.editTextStepSize)).getText().toString());
float required = distance / stepSize;
float interval = duration * 1000 / required;
ToneGenerator toneGenerator = new ToneGenerator(AudioManager.STREAM_SYSTEM, ToneGenerator.MAX_VOLUME);
timer.schedule(new TimerTask() {
@Override
public void run() {
toneGenerator.startTone(ToneGenerator.TONE_CDMA_PIP, 150);
}
}, 0, (long)interval);
}else{
button.setText("START");
timer.cancel();
timer.purge();
timer = new Timer();
running = false;
}
}catch (Exception e){
e.printStackTrace();
}
}
});
Upvotes: 0
Reputation: 6499
Sorry, there is no pause and resume method in countdownTimer class.
When you want to start then you can use
timer.start()
and when you want to stop then you can call
timer.cancel()
Upvotes: 1