Derek MC
Derek MC

Reputation: 376

Trying to make a timer loop

I'm trying to make a count down time loop. I want it to make it loop till "int x" reaches 100. I've tried adding a do/while and a for loop but I think I'm doing it wrong. Any suggestions? Thanks for looking. :-) `

    public class MainActivity extends Activity{

    Button buttonStartTime;

             // clicking this button will start time count down 
TextView textViewShowTime;              // will show the time 
TextView shots;
CountDownTimer countDownTimer;          // built in android class CountDownTimer
long totalTimeCountInMilliseconds;      // total count down time in milliseconds 
long timeBlinkInMilliseconds;           // start time of start blinking 
boolean blink;                          // controls the blinking .. on and off 
static int x = 3;

int whole = 100;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getReferenceOfViews ();                         // get all views 
    setActionListeners ();                          // set action listerns 

    totalTimeCountInMilliseconds = 60 * 1000;      // time count for 3 minutes = 180 seconds
    timeBlinkInMilliseconds = 10 * 1000;            // blink starts at 1 minutes = 60 seconds

}



private void setActionListeners() {

    buttonStartTime.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {



            textViewShowTime.setTextAppearance(getApplicationContext(), R.style.normalText);



            countDownTimer = new CountDownTimer(totalTimeCountInMilliseconds, 500) {
                // 500 means, onTick function will be called at every 500 milliseconds 

                @Override
                public void onTick(long leftTimeInMilliseconds) {
                    long seconds = leftTimeInMilliseconds / 1000;

                    if ( leftTimeInMilliseconds < timeBlinkInMilliseconds ) {
                        textViewShowTime.setTextAppearance(getApplicationContext(), R.style.blinkText);
                        // change the style of the textview .. giving a red alert style 

                        if ( blink ) {
                            textViewShowTime.setVisibility(View.VISIBLE);
                            // if blink is true, textview will be visible
                        } else {
                            textViewShowTime.setVisibility(View.INVISIBLE);
                        }

                        blink = !blink;         // toggle the value of blink
                    }

                    textViewShowTime.setText(String.format("%02d", seconds / 60) + ":" + String.format("%02d", seconds % 60));
                    // format the textview to show the easily readable format
                }

                @Override
                public void onFinish() {
                x++;
                    // this function will be called when the timecount is finished
                    textViewShowTime.setText("SHOT!!!");
                    textViewShowTime.setVisibility(View.VISIBLE);


                }

            }


            .start();


        }


    });


}



private void getReferenceOfViews() {

    buttonStartTime = (Button) findViewById(R.id.btnStartTime);
    textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
}

}`

Upvotes: 1

Views: 256

Answers (1)

vipul mittal
vipul mittal

Reputation: 17401

totalTimeCountInMilliseconds =1000 * 1000;      // time count for 3 minutes = 180 seconds
timeBlinkInMilliseconds = 10 * 1000;            // blink starts at 1 minutes = 60 seconds



new CountDownTimer(totalTimeCountInMilliseconds , timeBlinkInMilliseconds )

above will call onTick 100 times

Upvotes: 1

Related Questions