1. Are you ready for the Galaxy S20? Here is everything we know so far!

Countdown Timer Issues - Please Help

Discussion in 'Android Development' started by M3D, Oct 26, 2010.

  1. M3D

    M3D Lurker
    Thread Starter

    Hello, Great forum.....But I have a problem with an App I'm currently working on and having a hard time finding the correct way of doing what I need, Its a simple countdown time that beginner at 2 Min and counts down to 0 then displays a message.............The problem is formatting the output time correctly as in 00:00:00 The following code is what I have found and use.

    You will see that in the OnTick I have two methods the second being commented out both methods seam to work however the first show the output as 120 and counts down to 0 then shows the message "And Halt!" I need the output to read correctly as 00:02:00 and then countdown to 00:00:00.

    The second one that's commented out shows the correct output as 00:00:00 however does not update however when the time is up it does show the message "And Halt"

    And help would be great this is kick my Ass...........I could do the same in C++ without thinking about it, but I'm very new to Java...................So be gentle, Thanks in advance

    Code (Text):
    1. package com.droidnova.android;
    2. import android.app.Activity;
    3. import android.os.Bundle;
    4. import android.os.CountDownTimer;
    5. import android.view.View;
    6. import android.widget.TextView;
    7.  
    8. public class pu2min extends Activity {
    9.  
    10. static TextView timeDisplay;
    11.   MyCount counter;
    12.   static long timeRemaining  = 0;
    13.  
    14.  
    15.   public void onCreate(Bundle savedInstanceState) {
    16.     super.onCreate(savedInstanceState);
    17.     setContentView(R.layout.pu2min);
    18.     timeDisplay = (TextView) findViewById(R.id.timer);
    19.     counter = new MyCount(120000, 1000);
    20.   }
    21.  
    22.   public void start(View view) {
    23.     counter.start();
    24.   }
    25.  
    26.   public static String formatTime(long millis) {
    27.       String output = "";
    28.       long seconds = millis / 1000;
    29.       long minutes = seconds / 60;
    30.       long hours = minutes / 60;
    31.       long days = hours / 24;
    32.       seconds = seconds % 60;
    33.       minutes = minutes % 60;
    34.       hours = hours % 24;
    35.  
    36.       String secondsD = String.valueOf(seconds);
    37.       String minutesD = String.valueOf(minutes);
    38.       String hoursD = String.valueOf(hours);
    39.  
    40.       if (seconds < 10)
    41.         secondsD = "0" + seconds;
    42.       if (minutes < 10)
    43.         minutesD = "0" + minutes;
    44.       if (hours < 10){
    45.         hoursD = "0" + hours;
    46.       }
    47.  
    48.       if( days > 0 ){
    49.               output = days +"d ";
    50.       }
    51.               output += hoursD + ":" + minutesD + ":" + secondsD;
    52.      
    53.       return output;
    54. }
    55.  
    56.  
    57.   public static class MyCount extends CountDownTimer {
    58.     public MyCount(long millisInFuture, long countDownInterval) {
    59.       super(millisInFuture, countDownInterval);
    60.     }
    61.  
    62.     public void onFinish() {
    63.       timeDisplay.setText("And Halt!");
    64.     }
    65.  
    66.     public void onTick(long millisUntilFinished) {
    67.       timeDisplay.setText("Time Remaining: " + millisUntilFinished / 1000);
    68.       //timeDisplay.setText("Time Remaining: " + formatTime(timeRemaining));
    69.     }    
    70.   }
    71. }
     



    1. Download the Forums for Android™ app!


      Download

       
  2. sscAndroid

    sscAndroid Newbie

    Hi,

    your second solution doesn't update because your
    Code (Text):
    1. static long timeRemaining = 0;
    never changes its value :)
     
  3. M3D

    M3D Lurker
    Thread Starter

    And what would be the best way of getting it to change its value and update correctly
     
  4. sscAndroid

    sscAndroid Newbie

    Well why don't you just call formatTime with your method's parameter ?

    Code (Text):
    1.  
    2.     public void onTick(long millisUntilFinished) {
    3.       timeDisplay.setText("Time Remaining: " + formatTime(millisUntilFinished));
    4.  
    5.     }
    6.  
     
  5. M3D

    M3D Lurker
    Thread Starter

    Then the output would start at 120 and countdown from there...... I would like the output to be 00:02:00 and then countdown
     
  6. sscAndroid

    sscAndroid Newbie

    I just tested your method and it's working fine....where's the problem ?
     
Loading...
Similar Threads - Countdown Timer Issues
  1. metroidRoid2
    Replies:
    2
    Views:
    497
  2. OM2
    Replies:
    11
    Views:
    1,307
  3. snezhok
    Replies:
    0
    Views:
    441
  4. Robert Richards01
    Replies:
    2
    Views:
    788
  5. Robert Richards01
    Replies:
    2
    Views:
    869
  6. App Update
    Replies:
    0
    Views:
    478
  7. Clandes
    Replies:
    2
    Views:
    1,441
  8. BasitAliHashmi
    Replies:
    1
    Views:
    853
  9. Nick McNamara
    Replies:
    4
    Views:
    2,337
  10. NoxMemory
    Replies:
    0
    Views:
    485

Share This Page

Loading...