Skip to content Skip to sidebar Skip to footer

Incrementing The Countdowntimer Android

have a question about CountDownTimer. I have to make an application that allows the user to increment the time time by +1, for every click of the button. Then after the button stop

Solution 1:

You should know that you don't have to manually call onFinish when time is up in the countdown class, neither do you have you to cancel in onFinish as one leads to the other or manually decrement anything. Try the following and see if it fits the task that you had in mind. Hope you can note that i don't see pragmatic reason to see if a button "isn't being clicked anymore".

publicclassMainActivityextendsActivity {

    Button incrementTime, startTime;
    public TextView timedisplay;
    longmillisInFuture=1000;

    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        incrementTime = (Button) findViewById(R.id.button2);
        startTime = (Button) findViewById(R.id.button3);
        timedisplay = (TextView) findViewById(R.id.mycounter);

        resetText();

        incrementTime.setOnClickListener(newOnClickListener() {

            publicvoidonClick(View v) {
                millisInFuture += 1000;
                resetText();
            }
        });

        startTime.setOnClickListener(newOnClickListener(){
            publicvoidonClick(View v) {
                CountDownTimerwavetimer=newmyTimer(millisInFuture + 3000, 1000).start();
                // ^ add 3 seconds.
            }
        });}

    protectedvoidresetText() {
        timedisplay.setText("Time Left: " + millisInFuture / 1000);
    }

    publicclassmyTimerextendsCountDownTimer  {

        privatelong millisActual;

        publicmyTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
            millisActual = millisInFuture - 3000;
        }

        @OverridepublicvoidonTick(long millisUntilFinished) {
            //v start showing the tick after 3 seconds.if (millisUntilFinished <= millisActual) {
                timedisplay.setText("Time Left: " + millisUntilFinished / 1000);                
            }
        }

        @OverridepublicvoidonFinish() {
            timedisplay.setText("Countdown Finished");
        }
    }

    @OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        returntrue;
    }
}

Solution 2:

Add the TextView with name 'timerText' in your activity xml file

Add following two global variables in the class

int repeatCounter=1;//incrementing for every 60 sec
CountDownTimer tripTimeCounter;

Add following method to the class & call it from activity onCreate or from any other place where you want

publicvoidstartTimeCounter(){
    tripTimeCounter=newCountDownTimer(60*1000, 1000){

        @OverridepublicvoidonFinish() {
            // TODO Auto-generated method stub
            repeatCounter=repeatCounter+1;
            startTimeCounter();//follow the recursion on finish of the limit of 60 seconds & increment the repeat counter
        }

        @OverridepublicvoidonTick(long millisUntilFinished) {
            // TODO Auto-generated method stub

            timerText.setText(formatMillis((repeatCounter*60)*1000-millisUntilFinished));
        }

    }.start();
}

To show the value in the time format use following method

 static public String formatMillis(long val) {
    StringBuilder                       buf=new StringBuilder(20);
    String                              sgn="";

    if(val<0) { sgn="-"; val=Math.abs(val); }

    append(buf,sgn,0,( val/3600000));
    append(buf,":",2,((val%3600000)/60000));
    append(buf,":",2,((val %60000)/1000));
    //append(buf,".",3,( val%1000));return buf.toString();
 }

Post a Comment for "Incrementing The Countdowntimer Android"