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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:15:46+00:00 2026-06-06T07:15:46+00:00

Trying to make a small countdown timer in my app but it’s not working.

  • 0

Trying to make a small countdown timer in my app but it’s not working.

Idea is to count down to a specific time. First by days and when it gets closer, by hours.

The following method is inside my Fragmentclass.java (so it’s a fragment)

@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onViewCreated(view, savedInstanceState);


        Calendar cal = Calendar.getInstance();
        cal.set(2012, 8, 29, 10, 0);
        long endTime = cal.getTimeInMillis(); 
        long currentTime = System.currentTimeMillis();
        long remaining = currentTime - endTime;                 
        long seconds = remaining / 1000 % 60;               

         new CountDownTimer(seconds, 1000) {
             TextView tv = (TextView)getActivity().findViewById(R.id.introTimeLeft);
             public void onTick(long millisUntilFinished) {

                 Calendar cal = Calendar.getInstance();
                 cal.set(2012, 8, 29, 10, 0);
                long endTime = cal.getTimeInMillis(); 
                long currentTime = System.currentTimeMillis();
                long remaining = currentTime - endTime;                 
                long hours = remaining / 3600000;
                long mins = remaining / 60000 % 60;
                long seconds = remaining / 1000 % 60;
                long days = hours / 24;
                String remainingText = String.valueOf(days) + " days";

                Log.i("countdown",String.valueOf(days));
                 tv.setText("Days left: " + days);
             }

             public void onFinish() {
                 Log.i("countdown","CD Finished");
                 tv.setText("CD Finished!");
             }
          }.start();
    }

Note that the textbox value is just for testing right now, but i can’t seem to get it to display the countdown.

Also is this the best approach?

  • 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-06T07:15:48+00:00Added an answer on June 6, 2026 at 7:15 am

    Here are the values of the variables if I run your code:

    endTime     = 1348902045437
    currentTime = 1340645325437
    remaining   = -8256720000
    seconds     = 0
    

    as you see seconds == 0, and this is the reason.

    Also note that the first argument in the CountDownTimer(long millisInFuture, long countDownInterval) constructor is

    The number of millis in the future from the call to start() until the
    countdown is done and onFinish() is called.

    So a fixed code would be:

    Calendar cal = Calendar.getInstance();
    cal.set(2012, 8, 29, 10, 0);
    long endTime = cal.getTimeInMillis(); 
    long currentTime = System.currentTimeMillis();
    long remaining = endTime - currentTime;
    
    new CountDownTimer(remaining, 1000) {
    ...
    

    UPDATE:

    It is odd it does not update the text view. The only idea I can propose is to run update calls on the main UI thread. It should be something like this:

    public void onTick(long millisUntilFinished) {
        final String days = .. // some code to generate days
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                tv.setText("Days left: " + days);
            }
        });
    }
    
    public void onFinish() {
        Log.i("countdown","CD Finished");
        getActivity().runOnUiThread(new Runnable() {
            public void run() {
                tv.setText("CD Finished!");
            }
        });
    }
    

    If this doesn’t help – then I’m out of ideas.

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

Sidebar

Related Questions

Im trying to make a small app in c++ that saves midifiles with this
Hey guys, I'm trying to make a small addition to a web app I
I am trying to make a small pizza order form, but I have a
I'm trying to make a small button in an Android app that lets the
I am trying to make small calculator in jquery but don't know where to
I am trying to make a small game like app that has some interaction,
I'm trying to make a small app (an image gallery from images from the
I am trying to make some small adjustments to a very big website (not
I'm trying to make a small android Jump and Run game but my problem
Trying to make a multilingual installer - the process is working in general, but

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.