0

So I'm trying to make a timer like a stopwatch but I'm a complete noob. I tried "combining" things from Here and Here.

The goal is to take user input for how long they want to set the timer for, then when the time is up it does stuff.

This is what I have so far:

package com.example.timer;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
public TextView text;
private final long interval = 1 * 1000;
EditText editTime1;
Button startButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editTime1 = (EditText)findViewById(R.id.editTime1);     
    startButton = (Button)findViewById(R.id.startButton);
    text = (TextView) this.findViewById(R.id.timer);
    startButton.setOnClickListener(new OnClickListener() { 

        public void onClick(View v) {
            //get the name from edittext and storing into string variable
            long timeVal = Long.parseLong(editTime1.getText().toString());
            countDownTimer = new MyCountDownTimer(timeVal, interval);
            text.setText(text.getText() + String.valueOf(timeVal / 1000));

            if (!timerHasStarted) {
                   countDownTimer.start();
                   timerHasStarted = true;
                   startButton.setText("STOP");
                  } else {
                   countDownTimer.cancel();
                   timerHasStarted = false;
                   startButton.setText("RESTART");
                  }
            }
        class MyCountDownTimer extends CountDownTimer {
          public MyCountDownTimer(long timeVal, long interval) {
           super(timeVal, interval);
          }

          @Override
          public void onTick(long millisUntilFinished) {
           text.setText("" + millisUntilFinished / 1000);
          }
          @Override
          public void onFinish() {
           text.setText("Times up");
          }
         }
        });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}
Community
  • 1
  • 1
cflinspach
  • 290
  • 1
  • 4
  • 16
  • What's the problem you face with this code? – SoulRayder Jan 15 '14 at 03:16
  • It doesn't count down. The button changes to stop but instead of a count down it just displays "Times up". If you hit the button again it changes to restart and displays "Times up0". – cflinspach Jan 15 '14 at 03:29

2 Answers2

1

Few things to note.

  1. The Activity expects the start time to be in milliseconds. If an input value greater than 1000 is given (ex 10 seconds i.e. 10000), the app shows the count down.

  2. The following two lines are placed incorrectly.

    countDownTimer = new MyCountDownTimer(timeVal, interval);
    text.setText(text.getText() + String.valueOf(timeVal / 1000));
    

    They should be executed only when the count down is started. But in the given implementation, they are run both at start as well as stop.

As a result, a new MyCountDownTimer is created when the count down is stopped, and the countDownTimer.cancel(); is called in this new object rather than the original object. So the count down continues.

Since the setText is performed both on start and stop, the timeVal is appended to the output. That is causing "Times up0" observed.

The updated onClick method is as follows.

    public void onClick(View v) {
        // get the name from edittext and storing into string variable
        long timeVal = Long.parseLong(editTime1.getText().toString());

        if (!timerHasStarted) {
            countDownTimer = new MyCountDownTimer(timeVal, interval);
            text.setText(text.getText() + String.valueOf(timeVal / 1000));
            countDownTimer.start();
            timerHasStarted = true;
            startButton.setText("STOP");
        } else {
            countDownTimer.cancel();
            timerHasStarted = false;
            startButton.setText("RESTART");
        }
    }
0

I found a way to do that.

basically I let user to input their own values.

layout : HH:MM:SS

then I get these values from layout then process like below :

EditText tv_hour = findViewById(R.id.timerHHValue);
        EditText tv_minute = findViewById(R.id.timerMMValue);
        EditText tv_second = findViewById(R.id.timerSSValue);

     long inputTime = TimeUnit.MINUTES.toMillis(minute) + TimeUnit.HOURS.toMillis(hour) + TimeUnit.SECONDS.toMillis(second);
    
            mCountDownTimer = new CountDownTimer(inputTime, 1000) {
                public void onTick(long millisUntilFinished) {
    
                    DecimalFormat f = new DecimalFormat("00");
                    long hours = (millisUntilFinished / 3600000) % 24;
    
                    long minutes = (millisUntilFinished / 60000) % 60;
    
                    long second = (millisUntilFinished / 1000) % 60;
    
                    tv_hour.setText(f.format(hours)+"");
                    tv_minute.setText(f.format(minutes)+"");
                    tv_second.setText(f.format(second)+"");
                }
    
    
                @Override
                public void onFinish() {
                    //    timerFinish.setVisibility(View.VISIBLE);
                    tv_hour.setText("00");
                    tv_minute.setText("00");
                    tv_second.setText("00");
    
    
                }
            }.start();
    
        }
Energy
  • 940
  • 13
  • 20