Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9031567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:45:49+00:00 2026-06-16T07:45:49+00:00

I am now making a game where at first the player will see the

  • 0

I am now making a game where at first the player will see the colors of the buttons, and then after 5 seconds the color of all buttons will blank out and user has to pick those that are with the same color so as to win.

For the 5 seconds of waiting, I have used the handler to wait for execution, and at the same time displaying the countdown of the 5 second by the CountDownTimer for the player to know how long is the remaining time.

If the user gives up the current game, he can click the restart button to start another game.

Everything runs fine, and when the player clicks the restart button, the program also know need to wait for 5 seconds before blanking out all the buttons’ color.

Question:

It is about the display of the 5 second countdown. If the user start 1 game, the countdown start counts. If the user presses the restart button again, eg, after 3 seconds, the original countdown does not terminate, instead, it will display both countdown at the same time. I would like to ask how to terminate the previous countdown if the player presses the restart button.

Codes:

protected void onCreate(Bundle savedInstanceState) 
{
        ...other actions

    restart.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            handler1.removeCallbacks(txtClearRun);
            SetNewQ();
        }
    });

    SetNewQ();      

} // end onCreate

private void SetNewQ() 
{       
    MyCount5sec counter5sec = new MyCount5sec(5000,1000);
    counter5sec.start();
    ...other actions below
}   

   public class MyCount5sec extends CountDownTimer
   {
       public MyCount5sec(long millisInFuture, long countDownInterval) {super(millisInFuture, countDownInterval);}
       @Override
       public void onFinish() 
       {
           ButtonRemainTime= (Button) findViewById(R.id.button_remaintime); 
           ButtonRemainTime.setText("Add oil!!");
           // game then start by blanking out all the colors
       }
       @Override
       public void onTick(long millisUntilFinished) 
       {
           ButtonRemainTime= (Button) findViewById(R.id.button_remaintime); 
           ButtonRemainTime.setText("" + millisUntilFinished/1000);
       }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T07:45:50+00:00Added an answer on June 16, 2026 at 7:45 am

    I know what your question is asking for but i’d personally adjust my setup in the following way. If your CountDownTimer class implements not much else, you can actually declare the class a field and instantiate it the one fell swoop.

    protected void onCreate(Bundle savedInstanceState) 
    {
            ...other actions
    
        restart.setOnClickListener(new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                handler1.removeCallbacks(txtClearRun);
                MyCount5sec.start(); // ----------changed this line
            }
        });
    
        MyCount5sec.start(); // ---------- changed this line
    
    } // end onCreate
    
    CountDownTimer MyCount5sec = new CountDownTimer(5000,1000) {
    
        @Override
        public void onTick(long millisUntilFinished) {
               ButtonRemainTime= (Button) findViewById(R.id.button_remaintime); 
               ButtonRemainTime.setText("" + millisUntilFinished/1000);         
        }
    
        @Override
        public void onFinish() {
               ButtonRemainTime= (Button) findViewById(R.id.button_remaintime); 
               ButtonRemainTime.setText("Add oil!!");
               // game then start by blanking out all the colors            
        }
    }; 
    

    The benefit of doing it like this is that you’d always be referring to the same object. you wouldn’t have to make a new timer on each onClick and then manage cancelling the other one. In fact, you wouldn’t even have to cancel at all because calling start on the timer early on this same timer object will just restart it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a game. Now if my player goes to another level the music
I have a game I'm making in .NET at the moment. Right now I
I'm now making unit-tests for already existing code. I faced the next problem: After
I am making a game that I want compatible for all older devices to
okay i'm making a game using c++ (for the engine) and openGL, now i've
I'm making my first Android game which is going to be a 3D arcade-ish
I'm making a game with my friend. Now there is a question about levels.
I am making yet another iOS game with cocos2d and am now wondering what
Possible Duplicate: 3 player card game turn system I'm making a GUI 3 player
I'm making my first game using Java on Android. I need to draw a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.