Skip to content Skip to sidebar Skip to footer

Android Countdowntimer - Adding Time Results In Multiple Timers Running

Im trying to make it so that my timer adds 5 seconds every time I press the button. I've learnt that I need to cancel the previous timer and create a new one for it to work the way

Solution 1:

Change this

publicvoidtimerCreation (){
    counter.cancel();
    MyCount counter = new MyCount(millisUntilFinishedInt + 5000,1);
    counter.start();
}

Into this

publicvoidtimerCreation (){
    counter.cancel();
    counter = new MyCount(millisUntilFinishedInt + 5000,1);
    counter.start();
}

With your current implementation, you are cancelling your member variable counter. Then you create a local variable with the same name and start that one. The next time you press your button, your member variable counter gets cancelled again (which is already cancelled) in order to create a new MyCount object and start that one. That is why you are ending up with multiple timers

Solution 2:

Changes: increase interal to 50 but but mandatory.

MyCountcounter=newMyCount(millisUntilFinishedInt + totalAddedTime, 50);

Align text view gravity to left android:gravity="left" so user cant feel its new timmer.

Tested Working Demo

MainActivity

publicclassMainActivityextendsAppCompatActivity {
    Context context;
    intscoreTeamA=0;
    String countDownTimer;
    longmillisUntilFinishedInt=5000;
    long milliseconds;
    long seconds;
    longtotalAddedTime=0;
    TextView text1;
    MyCountcounter=newMyCount(millisUntilFinishedInt + totalAddedTime, 50);

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

        text1 = (TextView) findViewById(R.id.foodName);
        text1.setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                timerCreation();
            }
        });
    }

    publicclassMyCountextendsCountDownTimer {

        publicMyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @OverridepublicvoidonTick(long millisUntilFinished) {

            millisUntilFinishedInt = millisUntilFinished;
            seconds = millisUntilFinishedInt / 1000;
            milliseconds = millisUntilFinishedInt - (millisUntilFinishedInt / 1000) * 1000;
            countDownTimer = "TIME: " + seconds + "." + milliseconds;
            text1.setText(countDownTimer);
        }

        @OverridepublicvoidonFinish() {
            countDownTimer = "TIME'S UP!";
            text1.setText(countDownTimer);
        }
    }

    publicvoidtimerCreation() {
        counter.cancel();
        MyCountcounter=newMyCount(millisUntilFinishedInt + 5000, 1);
        counter.start();
    }

    //method that is called when button is pressedpublicvoidthreePoints(View v) {
        timerCreation();
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/base"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_margin="10dp"android:background="@color/white"android:orientation="horizontal"android:weightSum="10"><TextViewandroid:id="@+id/foodName"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:hint="Food name"android:gravity="left"android:inputType="textCapWords"android:textColor="@color/colorPrimaryDark"android:textColorHint="@color/colorPrimaryDark"android:textSize="32sp"android:layout_marginLeft="20dp" /></RelativeLayout>

Post a Comment for "Android Countdowntimer - Adding Time Results In Multiple Timers Running"