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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:36:15+00:00 2026-05-19T02:36:15+00:00

Can someone give a simple example of updating a textfield every second or so?

  • 0

Can someone give a simple example of updating a textfield every second or so?

I want to make a flying ball and need to calculate/update the ball coordinates every second, that’s why I need some sort of a timer.

I don’t get anything from here.

  • 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-19T02:36:15+00:00Added an answer on May 19, 2026 at 2:36 am

    ok since this isn’t cleared up yet there are 3 simple ways to handle this.
    Below is an example showing all 3 and at the bottom is an example showing just the method I believe is preferable. Also remember to clean up your tasks in onPause, saving state if necessary.

    
    import java.util.Timer;
    import java.util.TimerTask;
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.os.Handler.Callback;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class main extends Activity {
        TextView text, text2, text3;
        long starttime = 0;
        //this  posts a message to the main thread from our timertask
        //and updates the textfield
       final Handler h = new Handler(new Callback() {
    
            @Override
            public boolean handleMessage(Message msg) {
               long millis = System.currentTimeMillis() - starttime;
               int seconds = (int) (millis / 1000);
               int minutes = seconds / 60;
               seconds     = seconds % 60;
    
               text.setText(String.format("%d:%02d", minutes, seconds));
                return false;
            }
        });
       //runs without timer be reposting self
       Handler h2 = new Handler();
       Runnable run = new Runnable() {
    
            @Override
            public void run() {
               long millis = System.currentTimeMillis() - starttime;
               int seconds = (int) (millis / 1000);
               int minutes = seconds / 60;
               seconds     = seconds % 60;
    
               text3.setText(String.format("%d:%02d", minutes, seconds));
    
               h2.postDelayed(this, 500);
            }
        };
    
       //tells handler to send a message
       class firstTask extends TimerTask {
    
            @Override
            public void run() {
                h.sendEmptyMessage(0);
            }
       };
    
       //tells activity to run on ui thread
       class secondTask extends TimerTask {
    
            @Override
            public void run() {
                main.this.runOnUiThread(new Runnable() {
    
                    @Override
                    public void run() {
                       long millis = System.currentTimeMillis() - starttime;
                       int seconds = (int) (millis / 1000);
                       int minutes = seconds / 60;
                       seconds     = seconds % 60;
    
                       text2.setText(String.format("%d:%02d", minutes, seconds));
                    }
                });
            }
       };
    
    
       Timer timer = new Timer();
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            text = (TextView)findViewById(R.id.text);
            text2 = (TextView)findViewById(R.id.text2);
            text3 = (TextView)findViewById(R.id.text3);
    
            Button b = (Button)findViewById(R.id.button);
            b.setText("start");
            b.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Button b = (Button)v;
                    if(b.getText().equals("stop")){
                        timer.cancel();
                        timer.purge();
                        h2.removeCallbacks(run);
                        b.setText("start");
                    }else{
                        starttime = System.currentTimeMillis();
                        timer = new Timer();
                        timer.schedule(new firstTask(), 0,500);
                        timer.schedule(new secondTask(),  0,500);
                        h2.postDelayed(run, 0);
                        b.setText("stop");
                    }
                }
            });
        }
    
        @Override
        public void onPause() {
            super.onPause();
            timer.cancel();
            timer.purge();
            h2.removeCallbacks(run);
            Button b = (Button)findViewById(R.id.button);
            b.setText("start");
        }
    }
    
    
    

    the main thing to remember is that the UI can only be modified from the main ui thread so use a handler or activity.runOnUIThread(Runnable r);

    Here is what I consider to be the preferred method.

    
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class TestActivity extends Activity {
    
        TextView timerTextView;
        long startTime = 0;
    
        //runs without a timer by reposting this handler at the end of the runnable
        Handler timerHandler = new Handler();
        Runnable timerRunnable = new Runnable() {
    
            @Override
            public void run() {
                long millis = System.currentTimeMillis() - startTime;
                int seconds = (int) (millis / 1000);
                int minutes = seconds / 60;
                seconds = seconds % 60;
    
                timerTextView.setText(String.format("%d:%02d", minutes, seconds));
    
                timerHandler.postDelayed(this, 500);
            }
        };
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test_activity);
    
            timerTextView = (TextView) findViewById(R.id.timerTextView);
    
            Button b = (Button) findViewById(R.id.button);
            b.setText("start");
            b.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Button b = (Button) v;
                    if (b.getText().equals("stop")) {
                        timerHandler.removeCallbacks(timerRunnable);
                        b.setText("start");
                    } else {
                        startTime = System.currentTimeMillis();
                        timerHandler.postDelayed(timerRunnable, 0);
                        b.setText("stop");
                    }
                }
            });
        }
    
      @Override
        public void onPause() {
            super.onPause();
            timerHandler.removeCallbacks(timerRunnable);
            Button b = (Button)findViewById(R.id.button);
            b.setText("start");
        }
    
    }
    
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone give me a simple example of a AnnotatedTimeLine visualization? All the examples
Can someone give me a simple example involving threads in this manner, please. Problem
Can someone give an example of a good time to actually use unsafe and
Can someone give me some example code that creates a surface with a transparent
When is it appropriate to use CoTaskMemAlloc? Can someone give an example?
How do I use GDI+ with C++Builder ? Can someone give me a simple
Can someone give some hints of how to delete the last n lines from
Guys, can someone give me a brief run through of how to change the
Title is the entire question. Can someone give me a reason why this happens?
I'm looking to learn Windows PowerShell. Can someone give me some really good references

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.