tags:

views:

407

answers:

2

Hi,

I am creating an appication for video recording and I overlayed the video preview with labels txtStatus and txtTime.

The camera button starts/stops the timer which periodically calls the UpdateGUI method. Running the debug I can see the timer is working - it calls updateGUI method every second but the method doesn't update the controls.

I would really appreciate if I could get any hint on how to fix this issue.

Here is the code:

This is the method which activates the timer:

private void startTimer()
{
 updateTimer = new Timer("TimerUpdate");
 updateTimer.scheduleAtFixedRate(new TimerTask(){
  public void run(){
   settings.IncreaseRecordingTime();
   updateGUI();

  }
 }, 0, 1000);
}

This is the updateGUI method:

private void updateGUI()
{
 setStatusLabel();
 String strTime = settings.GetTimerString(); //strTime changes every second (it works as expected)
 txtTimer.setText(strTime);//the text doesn't change!
}

And this is the method which is called when the button is pressed:

private boolean onCaptureButton()
{
 settings.CaptureAction();
 videoPreview.setFrameCallback(settings);
 updateGUI();//here the function updateGUI() works as expected - it changes the txtStatus text from "Preview" to "Recording"
 setTimer();
 return false;
}

I added some comments as well (don't know why updateGUI() works when it is called at onCaptureButton() method and doesn't work when called inside the timer method).

A: 

After updating each control call control.Refresh() or form.Refresh() - this forces the controls to be redrawn immediately.

In your case

txtTimer.setText(strTime);//the text doesn't change!
txtTimer.Refresh();
I tried to use txtTimer.refreshDrawableState(); but it doesn't work.
niko
+1  A: 

Timers run on a timer thread. You should only update the GUI from the UI thread. There are a couple of ways of doing this. One of the easiest is AsyncTasks or posting events to the the UI handler. Some functions work when called on nonGUI threads but that isn't well documented. I have never seen the refresh function. I have always used invalidate (UI thread) or postInvalidate (background thread). I thought that setText called that internally though.

hacken