Home » Android » How to update variable and show in UI on 1000 millie intervals [duplicate]

How to update variable and show in UI on 1000 millie intervals [duplicate]

Posted by: admin October 28, 2017 Leave a comment


This question already has an answer here:


You can use a Handler to create a repeating loop which could update your variable/UI,

First we need a Handler that starts the Runnable after 1000ms

private Handler handler = new Handler();
handler.postDelayed(runnable, 1000);

And we also need the Runnable for the Handler

private Runnable runnable = new Runnable() {
   public void run() {
      /* do what you need to do */
      /* and here comes the "trick" */
      handler.postDelayed(this, 1000);


The “handler.postDelayed(this,1000);” at the end of the runnable allows this to recall itself and thus repeat every 1000ms.

An example of me using this (Splash screen):

private static final int SPLASH_DELAY = 1000; //Delay before splash advances
private static final int RETRY_TIME = 500; //Wait time if service has not started yet, before trying again.
private Handler handler;

// Only enter app once service is ready to go
private final Runnable enterAppRunable = new Runnable() {
    public void run() {
        if(hasServiceStarted()) enterApplication();
        else handler.postDelayed(enterAppRunable, RETRY_TIME);

protected void onCreate(Bundle savedInstanceState) {
    handler = new Handler();
    //Add delay to splash screen
    handler.postDelayed(enterAppRunable, SPLASH_DELAY);

Hope that clears things up a little.