Get variable from countdown and set it to TextView

sophin :

I need your help to get a variable from the countdown and set that value to text view. Suppose if countdown stopped at 0:40 sec and I want to put that number to text view.

So I'm using Seekbar to update the time with progress and a textview. And suppose I stopped at a certain number, next time I start again, let the number start from when it stopped. I have uploaded the image of output. Thanks

I learned to create this app from udemy online tutorial. Its called Complete android developer course- Build 23 Apps!!. Its lesson 38- App Egg timer.

This is my MainActivity.java file

public class MainActivity extends AppCompatActivity {
    TextView timerTv;
    SeekBar timerSeekBar;
    Button startBtn;

    CountDownTimer countDownTimer;

    boolean counterisActive = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        timerTv = (TextView) findViewById(R.id.countdowntimertextview);
        timerSeekBar = (SeekBar) findViewById(R.id.timerSeekBar);

        startBtn = (Button) findViewById(R.id.startBtn);

        timerSeekBar.setMax(300);
        timerSeekBar.setProgress(20);

        timerSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                updateTimer(progress);

                Log.i("Seekbar changes", String.valueOf(progress), null);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    public void resetTimer(){


        //This is where I want to set the text.
        timerTv.setText("Trying to get text from countdown!!");

        startBtn.setText("START!");
        timerSeekBar.setEnabled(true);
        countDownTimer.cancel();
        timerSeekBar.setProgress(20);
        counterisActive = false;

    }

    public void buttonClicked(View view){

        if(counterisActive){
            resetTimer();

        }else {
            counterisActive =  true;
            timerSeekBar.setEnabled(false);
            startBtn.setText("STOP!");

            Log.i("Button Clicked", "Clicked");
             countDownTimer = new CountDownTimer(timerSeekBar.getProgress() * 1000 + 100, 1000) {


                @Override
                public void onTick(long millisUntilFinished) {
                    updateTimer((int) millisUntilFinished / 1000);
                }

                @Override
                public void onFinish() {
                    MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.templebell);
                    mediaPlayer.start();
                    Log.i("Timer Finished", "Done!!");
                    resetTimer();
                }
            }.start();
        }
    }

    public void updateTimer(int secondLefts){
        int minutes = secondLefts / 60;
        int seconds = secondLefts - (minutes * 60);

        String secondString = Integer.toString(seconds);

        if(seconds <= 9) {
            secondString = "0" + seconds;
        }

        timerTv.setText(minutes + " : " + secondString );

    }

}

enter image description here

J-Abdo :

I think you need to pause the timer. First create a global long variable in your activity;

long timerProgress;

Change your restProgress() method like below, or you can add new method pauseTimer().

public void restTimer(){
        //This is where I want to set the text.

        updateTimer((int) timerProgress/1000);
        startBtn.setText("START!");
        timerSeekBar.setEnabled(true);
        countDownTimer.cancel();
        timerSeekBar.setProgress((int) timerProgress/1000);
        counterisActive = false;
    }

Know add this line to your override method onTick.

@Override
public void onTick(long millisUntilFinished) {
         updateTimer((int) millisUntilFinished / 1000);
         // add this line for store progress timer.
         timerProgress = millisUntilFinished;
     }
  • You can add another button one for pause and other for rest Timer.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=401636&siteId=1