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 8738989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:50:56+00:00 2026-06-13T10:50:56+00:00

I created a game with timer, I am trying to make that when the

  • 0

I created a game with timer, I am trying to make that when the timer is end the player will see alert popup or any kind of pop up that say “level complete”, your points are xxx and a button for next level. I tried something but the time over but no popup.
any idea?

Time Class: work fine.

public class Time {

private String time;
private boolean isDone;

public Time() {
    super();
    isDone=false;
}

CountDownTimer count = new CountDownTimer(5000, 1000) {

public void onTick(long millisUntilFinished) {

    int seconds = (int) (millisUntilFinished / 1000);
    int minutes = seconds / 60;
    seconds = seconds % 60;
    String tempSec=Integer.toString(seconds);
    if (tempSec.length()==1){
        tempSec="0"+tempSec;
    }
    time="Time Left: " + minutes + ":"+tempSec;
 }

 public void onFinish() {
    setDone(true);
}

}.start(); 

This is the Activity class:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    club=new Club();
    clubView = new ClubView(this, club);
    mole=new Mole();
    stageView=new StageView(this);
    moleView=new MoleView(this,mole);
    pointsView=new PointsView(this);

    time=new Time();
    timerView=new TimerView(this, time);

    allViews=new AllViews(this);
    allViews.setViews(stageView, moleView, pointsView, timerView,clubView);

    setContentView(allViews);
    allViews.setOnTouchListener((View.OnTouchListener)this);

    if (timerView.getTime().isDone()){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Level Complete");
        builder.setMessage("your score is"+pointsView.getPoint());
        AlertDialog dialog = builder.create();
        dialog.show();
    }

}
  • 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-13T10:50:57+00:00Added an answer on June 13, 2026 at 10:50 am

    The point is that the timer takes some while to run out, and you are checking just once if the timer is done:

    if (timerView.getTime().isDone()){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Level Complete");
        builder.setMessage("your score is"+pointsView.getPoint());
        AlertDialog dialog = builder.create();
        dialog.show();
    }
    

    A better options would be to create some kind of loop, but this is forbidden! Because you would block the main thread.

    The next option you have is to create some kind of listener. The listener will do some kind of callback to your activity to tell “i’m done”. This is often done using interfaces,

    Small Example:

    public class Time {
    
        private String time;
        private boolean isDone;
        private TimerCallback timerCallback;
    
        public Time(TimerCallback t) {
            this.timerCallback = t;
            isDone = false;
        }
    
        public interface TimerCallback {
            abstract void onTimerDone();
        }
    
        CountDownTimer count = new CountDownTimer(5000, 1000) {
    
            public void onTick(long millisUntilFinished) {
    
                int seconds = (int) (millisUntilFinished / 1000);
                int minutes = seconds / 60;
                seconds = seconds % 60;
                String tempSec = Integer.toString(seconds);
                if (tempSec.length() == 1) {
                    tempSec = "0" + tempSec;
                }
                time = "Time Left: " + minutes + ":" + tempSec;
            }
    
            public void onFinish() {
                setDone(true);
                timerCallback.onTimerDone();
            }
    
        }.start();
    
    }
    

    And your activity would look like this:

    public class myActivity extends Activity implements TimerCallback {
    //I have now clue how your activity is named but it's just an example!
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
    
            club = new Club();
            clubView = new ClubView(this, club);
            mole = new Mole();
            stageView = new StageView(this);
            moleView = new MoleView(this, mole);
            pointsView = new PointsView(this);
    
            //Because we are implementing the TimerCallback interface "this" is a valid argument
            //this can be cast to TimerCallback:  "(TimerCallback) this"
            time = new Time(this);
            timerView = new TimerView(this, time);
    
            allViews = new AllViews(this);
            allViews.setViews(stageView, moleView, pointsView, timerView, clubView);
    
            setContentView(allViews);
            allViews.setOnTouchListener((View.OnTouchListener) this);
    
    
        }
    
        //We must add this method, because we have implemented the TimerCallback interface!
        public void onTimerDone(){
            //You could remove the isDone check because it is not really necessary
            if (timerView.getTime().isDone()) {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage("Level Complete");
                builder.setMessage("your score is" + pointsView.getPoint());
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        }
    
    }
    

    I have posted a very similar answer here, but in this question there is no timer but some kind of a “gameover” event.
    How do I perform a continuous check on andorid of the returned value of another class?

    And a wiki link about the listener pattern, or observer pattern
    http://en.wikipedia.org/wiki/Observer_pattern

    Rolf

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

Sidebar

Related Questions

I'm trying to make a small game using (free)GLUT. I know that it's old
I've created a game which gives a score at the end of the game,
I've been trying to make a little simple game just to test my logics,
I'm trying to make an NDK-based game work on Android ICS. It worked fine
So I'm trying to make a game where I spawn 20 enemies. The enemies
I'm trying to make a Tetris-like game in XNA, and currently I'm thinking of
I'm trying to make an easy side scrolling game just to learn the ropes
My question is essentially that I am trying to make a universal Ajax function
I am trying to make a simple 3D game for windows with XNA and
I am trying to create a form that creates a game and game_players at

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.