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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:44:08+00:00 2026-06-05T07:44:08+00:00

I have two buttons Start and Pauseand one Textview. The problem I have is

  • 0

I have two buttons “Start” and “Pause”and one Textview.

The problem I have is when I press the Pause button and then press start button after, the stopwatch doesn’t begin from where it left off.

Thanks for the help

public class StopWatch extends Activity {
    /** Called when the activity is first created. */

    private TextView timeflg;
    private Button startButton;
    private Button pauseButton;
    private long mStartTime = 0L;
    private Handler mHandler = new Handler();
    long millis = 0L;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (mStartTime == 0L) {
            mStartTime = SystemClock.uptimeMillis();
            mHandler.removeCallbacks(mUpdateTimeTask);
            mHandler.postDelayed(mUpdateTimeTask, 0);


            }

            timeflg = (TextView) findViewById(R.id.timeflag);

            startButton = (Button) findViewById(R.id.start);
            startButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view){

                    mHandler.postDelayed(mUpdateTimeTask, 0);

                }
            });

            pauseButton = (Button) findViewById(R.id.pause);
            pauseButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view){

                    mHandler.removeCallbacks(mUpdateTimeTask);

                }
            });

    }

    private Runnable mUpdateTimeTask = new Runnable() {
            public void run() {

                    millis = SystemClock.uptimeMillis() - mStartTime;

                    int seconds = (int) (millis / 1000);
                    int minutes = seconds / 60;
                    seconds = seconds % 60;
                    int milliseconds = (int) (millis%1000);
                    timeflg.setText("" + minutes + ":" + String.format("%02d",seconds)+ ":" 
                    + String.format("%03d",milliseconds));
                    mHandler.postDelayed(this,0);
            }

    };



}
  • 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-05T07:44:11+00:00Added an answer on June 5, 2026 at 7:44 am

    There is the problem:

    millis = SystemClock.uptimeMillis() - mStartTime;

    mStartTime is initialized when activity is created.
    Then presented time is calculated since mStartTime.

    EDIT:
    I see you still don’t get it.

    Initialize mStartTime when pressing start Button, it’s current lapse start time:

            startButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view){
                    mStartTime = SystemClock.uptimeMillis();
                    mHandler.postDelayed(mUpdateTimeTask, 0);
                }
            });
    

    Calculate mCurrenLapse time.
    Display time is time of past lapses and time of current lapse:

        private Runnable mUpdateTimeTask = new Runnable() {
            public void run() {
                    mCurrentLapse = SystemClock.uptimeMillis() - mStartTime;
                    //
                    long displayTime = mTime + mCurrentLapse;
                    .
                    .
                    .
    

    Total time is time of all lapses, when pause is clicked then current time lapse is added to total time:

            pauseButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view){
                    mTime += mCurrentLapse;
                    mHandler.removeCallbacks(mUpdateTimeTask);
                }
            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity with two buttons, start and stop. If the user press
In Application i have two buttons in which one button is used as a
If I have two buttons, one to start a song being played and one
I have two buttons on my application. One button starts a new Game (lets
I have two buttons, one for start recording and another for stop recording. i
I have two buttons with the same ID: <button type=submit onclick=//do something id=theID>button 1</button>
I have two buttons for my form - a Save changes button and a
I have two buttons in my first android frame. ok button and cancel button.
Right now I have two buttons. Each one needs to produce a different sound.
I have a simple form with two buttons (Start and Stop) and a DataGridView.

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.