Richy
Richy

Reputation: 1

Android CountDownTimer not rendering properly

I have a simple Android countdown timer whose output is distorted. I am trying to create a simple game where the user has to click a button repeatidly which in turn changes a drawable image from one to another through a course of 8 images. The drawables render fine, but the countdown timer (which should could from 30 seconds downwards), seems to stick and/or generate incorrect numbers which do not represent the current amount of time passed. Has anyone any advice on why this is happening or what I should investigate to mend this issue?

MultitapGame.java

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

    final View btn = findViewById(R.id.gameButton);
    final View img = findViewById(R.id.game1);

    final TextView mTextField = (TextView) findViewById(R.id.gameCDT);
    final TextView mTextField2 = (TextView) findViewById(R.id.gameCDT2);
    final View mTextField3 = findViewById(R.id.gameCDT3);
    final View mTextField4 = findViewById(R.id.gameCDT4);


    img.setBackgroundResource(R.drawable.image1);

        btn.setOnClickListener(new OnClickListener() {

            public int count = 0;


        @Override
        public void onClick(View v) {

            setCount(getCount() + 1);
            new CountDownTimer(30000, 1000) {

                 public void onTick(long millisUntilFinished) {

                     int timeLeft =  (int) (millisUntilFinished / 1000) ;

                     ((TextView) mTextField).setText("seconds remaining: ");
                     final TextView mTextField2 = (TextView) findViewById(R.id.gameCDT2);
                        mTextField2.setText(String.valueOf(timeLeft + ""));

                 }

                 public void onFinish() {

                     int numTaps = count;
                     if(numTaps<150)
                     {
                         ((TextView) mTextField3).setText("Unlucky chucky! ");
                         ((TextView) mTextField4).setText("taps done: " + numTaps);
                     }
                     else
                     {
                         ((TextView) mTextField3).setText("Congrats Bro :-) !");
                         ((TextView) mTextField4).setText("taps done: " + numTaps);
                     }
                 }
              }.start();


            if(count==15)
            {
            img.setBackgroundResource(R.drawable.image2);}
            if(count==30)
            {
            img.setBackgroundResource(R.drawable.image3);}
            if(count==45)
            {
            img.setBackgroundResource(R.drawable.image4);}
            if(count==60)
            {
            img.setBackgroundResource(R.drawable.image5);}
            if(count==75)
            {
            img.setBackgroundResource(R.drawable.image6);}
            if(count==90)
            {
            img.setBackgroundResource(R.drawable.image5);}
            if(count==100)
            {
            img.setBackgroundResource(R.drawable.image6);}
            if(count==110)
            {
            img.setBackgroundResource(R.drawable.image5);}
            if(count==115)
            {
            img.setBackgroundResource(R.drawable.image6);}
            if(count==118)
            {
            img.setBackgroundResource(R.drawable.image5);}
            if(count==121)
            {
            img.setBackgroundResource(R.drawable.image6);}
            if(count==123)
            {
            img.setBackgroundResource(R.drawable.image5);}
            if(count==125)
            {
            img.setBackgroundResource(R.drawable.image6);}
            if(count==126)
            {
            img.setBackgroundResource(R.drawable.image5);}
            if(count==127)
            {
            img.setBackgroundResource(R.drawable.image6);}

            if(count==130)
            {
            img.setBackgroundResource(R.drawable.image7);}
            if(count==140)
            {
            img.setBackgroundResource(R.drawable.image1);}
            if(count==160)
            {
                Intent i = new Intent();
                i.setClassName("com.B00512756.angertwo",
                        "com.B00512756.angertwo.Strategies");
                startActivity(i);
                finish();}

        }


        public int getCount() {
            return count;
        }


        public void setCount(int count) {
            this.count = count;
        }
        });
}

}

Upvotes: 0

Views: 601

Answers (1)

Erol
Erol

Reputation: 6526

I think the issue is with implementing CountDownTimer inside your onClick method.

I would suggest implementing it as a subclass that extends CountDownTimer, such as:

public class MyCounter extends CountDownTimer{

        public MyCounter(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

                 @Override
                 public void onTick(long millisUntilFinished) {

                     mTextField.setText("seconds remaining: ");
                     mTextField2.setText(String.valueOf((int) (millisUntilFinished/1000) + ""));

                 }

                 @Override
                 public void onFinish() {

                     int numTaps = count;
                     if(numTaps<150)
                     {
                         mTextField3.setText("Unlucky chucky! ");
                         mTextField4.setText("taps done: " + numTaps);
                     }
                     else
                     {
                         mTextField3.setText("Congrats Bro :-) !");
                         mTextField4.setText("taps done: " + numTaps);
                     }
                 }

    }

Also, it is a better idea to make the code inside onTick() as short as possible because the calls to onTick(long) are synchronized to this object. So one call to onTick(long) won't ever occur before the previous callback is complete. This is only relevant when the implementation of onTick(long) takes an amount of time to execute that is significant compared to the countdown interval. Thus, if it takes for your onTick() code to execute longer than a second, you will have lags in your countdown. That's why I made it shorter.

It is a better idea to initialize your TextViews inside your onCreate() and declare them as public class variables so that you will not have to do this everytime your counter ticks or finishes.

Next step is to initialize your timer in onCreate:

final MyCounter timer = new MyCounter(30000,1000);

Finally, you need to start it in your onClick() method:

timer.start();

Upvotes: 1

Related Questions