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

  • Home
  • SEARCH
  • 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 704293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:55:33+00:00 2026-05-14T03:55:33+00:00

I want to create an incrementing second timer like a stopwatch. So I want

  • 0

I want to create an incrementing second timer like a stopwatch.

So I want to be able to display the seconds and minutes incrementing in the format 00:01…

Google only brings up 24 hour clock examples, I was wondering could anyone get me started with an example or tutorial of what I want to do?

Edit:

Here is what I have using the Chronometer in Android so far

In onCreate()

    secondsT = 0;
    elapsedTimeBeforePause = 0;

    stopWatch.start();
    startTime = SystemClock.elapsedRealtime();
    stopWatch.setBase(elapsedTimeBeforePause);

     stopWatch.setOnChronometerTickListener(new OnChronometerTickListener(){
            @Override
            public void onChronometerTick(Chronometer arg0) {
                //countUp is a long declared earlier
                secondsT = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
                String asText = (secondsT / 60) + ":" + (secondsT % 60); 
                //textGoesHere is a TextView
                ((TextView)findViewById(R.id.time)).setText(asText);
            }
     });

In onDestroy()

@Override
public void onDestroy() {

    inCall = false;
    elapsedTimeBeforePause = SystemClock.elapsedRealtime() - stopWatch.getBase();

    super.onDestroy();
}

The above compiles and runs but the TextView never increments, it always stays at 0, can anyone see why?

  • 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-05-14T03:55:33+00:00Added an answer on May 14, 2026 at 3:55 am

    I’m assuming you aren’t aware of the Android Chronometer – it already has a basic stopwatch function. You need to work with its peculiarities a bit, but it’s not hard to get it to do what you want once you understand how it works.

    There are a few ways that time is calculated on the phone, but the two main ones are:

    1. The “real time”, such as right now according to my computer clock, it is 11:23am in England. However, this can change if my computer contacts a time server and is told it has the wrong time, or if I were travelling with a laptop and crossed a timezone boundary. Using this would wreak havoc with your stopwatch as the measured time could change at any time.

    2. The “elapsed time since boot”, which is the number of milliseconds since the phone was switched on. This number doesn’t bear any relation to the real time it is, but it will behave in a perfectly predictable manner. This is what the Android Chronometer uses.

    The Chronometer is essentially a ‘count up’ timer, comparing the current SystemClock.elapsedRealtime() against the elapsedRealtime() that was set fot its base time. The difference between the two, divided by 1000, is the number of seconds since the timer was started. However, if you stop the timer and then start it again, you will get a counter-intuitive result – the timer will show the elapsed time as if it had never stopped. This is because you need to adjust its base time to take into consideration the time it was stopped. This is simple to do:

    // When you're stopping the stopwatch, use this
    // This is the number of milliseconds the timer was running for
    elapsedTimeBeforePause = SystemClock.elapsedRealtime() - timer.getBase();
    
    // When you're starting it again:
    timer.setBase(SystemClock.elapsedRealtime() - elapsedTimeBeforePause);
    

    Edit: Here is the full code for a basic stopwatch, which displays your time in a TextView rather than the Chronometer widget declared in your XML file.

    public class TestProject extends Activity {
        TextView textGoesHere;
        long startTime;
        long countUp;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            Chronometer stopWatch = (Chronometer) findViewById(R.id.chrono);
            startTime = SystemClock.elapsedRealtime();
    
            textGoesHere = (TextView) findViewById(R.id.textGoesHere);
            stopWatch.setOnChronometerTickListener(new OnChronometerTickListener(){
                @Override
                public void onChronometerTick(Chronometer arg0) {
                    countUp = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
                    String asText = (countUp / 60) + ":" + (countUp % 60); 
                    textGoesHere.setText(asText);
                }
            });
            stopWatch.start();
        }
    }
    

    In your main.xml you need to have this

    <Chronometer android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/chrono"
        android:visibility="gone"/>
    

    There’s undoubtedly a way to get the Chronometer to work without declaring it in the XML file, but the constructor Chronometer stopwatch = new Chronometer(this); didn’t work properly.

    The above code displays the elapsed time in a very basic way. For example, if only 5 seconds have gone by, it will show 0:5 rather than the 0:05 you probably want. Fixing that is not hard to do, but I’ll leave that for you to work out! 🙂

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

Sidebar

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.