By user1718339


2012-10-03 21:02:22 8 Comments

I want to make a simple timer in Android that updates a TextView every second. It simply counts seconds like in Minesweeper.

The problem is when i ignore the tvTime.setText(...) (make it //tvTime.setText(...), in LogCat will be printed the following number every second. But when i want to set this number to a TextView (created in another Thread), the program crashes.

Does anyone have an idea how to solve this easily?

Here's the code (method is called on startup):

private void startTimerThread() {
    Thread th = new Thread(new Runnable() {
        private long startTime = System.currentTimeMillis();
        public void run() {
            while (gameState == GameState.Playing) {
                System.out.println((System.currentTimeMillis() - this.startTime) / 1000);
                tvTime.setText("" + ((System.currentTimeMillis() - this.startTime) / 1000));
                try {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    th.start();
}

EDIT:

Finally, I got it. Here is the solution, for those who are interested in.

private void startTimerThread() {       
    Thread th = new Thread(new Runnable() {
        private long startTime = System.currentTimeMillis();
        public void run() {
            while (gameState == GameState.Playing) {                
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvTime.setText(""+((System.currentTimeMillis()-startTime)/1000));
                    }
                });
                try {
                    Thread.sleep(1000);
                } 
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    th.start();
}

4 comments

@Jorgesys 2017-03-29 23:12:20

As an option use runOnUiThread() to change de views properties in the main thread.

  runOnUiThread(new Runnable() {
        @Override
        public void run() {       
                textView.setText("Stackoverflow is cool!");
        }
    });

@dennisdrew 2012-10-03 21:23:50

Alternatively, you can also just do this in your thread whenever you want to update a UI element:

runOnUiThread(new Runnable() {
    public void run() {
        // Update UI elements
    }
});

@madoke 2012-12-20 11:56:48

thanks, this was exactly what i was looking for

@Vadorequest 2013-11-17 11:45:54

Is it bad practice? Use a Handler is better?

@Meher 2014-04-26 18:50:16

yes..handler is a better option.Handler is from API 1.

@zienkikk 2012-10-03 21:21:13

You cannot access UI elements from non-UI threads. Try surrounding the call to setText(...) with another Runnable and then look into the View.post(Runnable) method.

@Chris Conway 2012-10-03 21:21:00

The UserInterface can only be updated by the UI Thread. You need a Handler, to post to the UI Thread:

private void startTimerThread() {
    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        private long startTime = System.currentTimeMillis();
        public void run() {
            while (gameState == GameState.Playing) {  
                try {
                    Thread.sleep(1000);
                }    
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(new Runnable(){
                    public void run() {
                       tvTime.setText("" + ((System.currentTimeMillis() - this.startTime) / 1000));
                }
            });
            }
        }
    };
    new Thread(runnable).start();
}

@nesimtunc 2013-06-14 13:06:30

In my case, there is a textview which is inside a row of listview; can't update its text property ... Any suggestions?

Related Questions

Sponsored Content

96 Answered Questions

[SOLVED] Close/hide the Android Soft Keyboard

25 Answered Questions

[SOLVED] Is there a way to run Python on Android?

34 Answered Questions

[SOLVED] What is the difference between a process and a thread?

47 Answered Questions

[SOLVED] Is there a unique Android device ID?

42 Answered Questions

[SOLVED] "implements Runnable" vs "extends Thread" in Java

64 Answered Questions

[SOLVED] How do I center text horizontally and vertically in a TextView?

  • 2009-01-11 00:27:55
  • pupeno
  • 1173040 View
  • 1918 Score
  • 64 Answer
  • Tags:   android textview

27 Answered Questions

[SOLVED] Making TextView scrollable on Android

  • 2009-11-17 13:43:03
  • Muhammad Maqsoodur Rehman
  • 538587 View
  • 725 Score
  • 27 Answer
  • Tags:   android scroll textview

77 Answered Questions

47 Answered Questions

[SOLVED] How do I update the GUI from another thread?

11 Answered Questions

[SOLVED] Proper use cases for Android UserManager.isUserAGoat()?

Sponsored Content