Search code examples
javaandroidstring-comparisonandroid-seekbar

Setting timer with seek bar


I have an app in which I have used minutes and seconds as a textview that changes with the dragging of seek bar and it is working fine. My only problem is that when it reaches to 10 minutes (I have set max to 600 which is ten minutes) it should be shown like this e.g 10:00 but unfortunately it is shown in like this 10:0 I have tested it in both emulator and genymotion below is my code

SeekBar seekBar = (SeekBar)findViewById(R.id.seekBarController);
    final TextView timerTextView = (TextView)findViewById(R.id.timerTextview);

    seekBar.setMax(600);
    seekBar.setProgress(30);
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {

            int minutes = progress / 60;
            int seconds = progress - minutes * 60;


            String secondString = Integer.toString(seconds);

            if (secondString == "0") {

                secondString = "00";

            }
                timerTextView.setText(Integer.toString(minutes) + ":" + secondString);

        }

Solution

  • Why don't you use

    if (seconds == 0) {
        secondString = "00";
    }
    

    or

    if (secondString.equalsIgnoreCase("0")) {
        secondString = "00";
    }
    

    String comparison might have some issue!