Answers for "start countdown and seekbar"

0

countdown timer with seekbar

public class MainActivity extends AppCompatActivity {
    private int seconds = 120; // two min
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        circularSeekbar = (SeekBar) findViewById(R.id.circularSeekbar);
        mTvTime = (TextView) findViewById(R.id.mTvTime);


        circularSeekbar.setMax(seconds);
        circularSeekbar.setProgress(seconds);
        circularSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser) {
                  seconds = progress;
                }

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                startTimer(seconds * 1000);
            }
        });

        startTimer(seconds * 1000);
    }


    private void startTimer(final long milisecods) {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        countDownTimer = new CountDownTimer(milisecods, 500) {

            // 500 means, onTick function will be called at every 500
            // milliseconds

            @Override
            public void onTick(long leftTimeInMilliseconds) {

                int barVal = (int) leftTimeInMilliseconds / 1000;
                circularSeekbar.setProgress(barVal);
                mTvTime.setText(String.format("%02d:%02d",
                        TimeUnit.MILLISECONDS.toMinutes(leftTimeInMilliseconds),
                        TimeUnit.MILLISECONDS.toSeconds(leftTimeInMilliseconds) -
                                TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(leftTimeInMilliseconds))
                ));
            }

            @Override
            public void onFinish() {

            }
        }.start();

    }
}
Posted by: Guest on June-10-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language