Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Sunday, February 21, 2016

Android timer updating a textview (UI)

Android timer updating a textview (UI)


I'm using a timer to create a stop watch. The timer works by increasing a integer value. I want to then display this value in the activity by constantly updating a textview.

Here's my code from the service where I try and update the activity's textview:

protected static void startTimer() {      isTimerRunning = true;       timer.scheduleAtFixedRate(new TimerTask() {          public void run() {              elapsedTime += 1; //increase every sec              StopWatch.time.setText(formatIntoHHMMSS(elapsedTime)); //this is the textview          }      }, 0, 1000);  }  

I got some kind of error about updating the UI in the wrong thread.

How can I adapt my code to accomplish this task of constantly updating the textview?

Thanks.

Answer by Pyrodante for Android timer updating a textview (UI)


TimerTask implements Runnable, which would make it a thread. You can not update the main UI thread directly from a different thread without some work. One thing you could do is use Async Task to create the timer and publish an update every second that will update the UI.

Answer by John Leehey for Android timer updating a textview (UI)


I'm assuming StopWatch.time is some static or public reference to your TextView. Instead of doing this, you should implement a BroadcastReceiver to communicate between your timer (which runs from a separate thread) and your TextView.

Answer by inazaruk for Android timer updating a textview (UI)


You should use Handler instead to update UI every X seconds. Here is another question that show an example: repeat a task with a time delay

Your approach doesn't work because you are trying to update UI from non-UI thread. This is not allowed.

Answer by Nirav for Android timer updating a textview (UI)


protected static void startTimer() {      isTimerRunning = true;       timer.scheduleAtFixedRate(new TimerTask() {          public void run() {              elapsedTime += 1; //increase every sec              mHandler.obtainMessage(1).sendToTarget();            }      }, 0, 1000);  };    public Handler mHandler = new Handler() {      public void handleMessage(Message msg) {          StopWatch.time.setText(formatIntoHHMMSS(elapsedTime)); //this is the textview      }  }  

Above code must work...

Note:mHandler must be created in your main thread.

Answer by DongXu for Android timer updating a textview (UI)


StopWatch.time.post(new Runnable() {      StopWatch.time.setText(formatIntoHHMMSS(elapsedTime));  });  

this code block is based on Handler but you don't need to create your own Handler instance.

Answer by Ofek Ron for Android timer updating a textview (UI)


You can use the following utility :

/**   * Created by Ofek on 19/08/2015.   */  public class TaskScheduler extends Handler {      private ArrayMap tasks = new ArrayMap<>();      public void scheduleAtFixedRate(final Runnable task,long delay,final long period) {          Runnable runnable = new Runnable() {              @Override              public void run() {                  task.run();                  postDelayed(this, period);              }          };          tasks.put(task, runnable);          postDelayed(runnable, delay);      }      public void scheduleAtFixedRate(final Runnable task,final long period) {          Runnable runnable = new Runnable() {              @Override              public void run() {                  task.run();                  postDelayed(this, period);              }          };          tasks.put(task, runnable);          runnable.run();      }      public void stop(Runnable task) {          Runnable removed = tasks.remove(task);          if (removed!=null) removeCallbacks(removed);      }    }  

Then anywhere in code that runs by the UI Thread you can use it simply like this:

TaskScheduler timer = new TaskScheduler();          timer.scheduleAtFixedRate(new Runnable() {              @Override              public void run() {                  time.setText(simpleDateFormat.format(GamePlay.instance().getLevelTime()));              }          },1000);  

Answer by Dmitriy_ for Android timer updating a textview (UI)


 timer.schedule(new TimerTask() {              @Override              public void run() {                    metPlaySound();                }          },1*1000);//1 sec  

Create timer = new Timer(); in onCreate method and goodDay


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.