0
votes

So I have a TextSwitcher that I want to update every second with the number of seconds it has been since the activity opened. Here is my code

public class SecondActivity extends Activity implements ViewFactory
{   
    private TextSwitcher counter;
    private Timer secondCounter;
    int elapsedTime = 0;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        // Create the layout
        super.onCreate(savedInstanceState);

        setContentView(R.layout.event);

        // Timer that keeps track of elapsed time
        counter = (TextSwitcher) findViewById(R.id.timeswitcher);
        Animation in = AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in);
        Animation out = AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out);
        counter.setFactory(this);
        counter.setInAnimation(in);
        counter.setOutAnimation(out);

        secondCounter = new Timer();
        secondCounter.schedule(new TimerUpdate(), 0, 1000);
    }

    /**
     * Updates the clock timer every second
     */
    public void updateClock()
    {        
        //Update time
        elapsedTime++;
        int hours = elapsedTime/360;
        int minutes = elapsedTime/60;
        int seconds = elapsedTime%60;

        // Format the string based on the number of hours, minutes and seconds
        String time = "";

        if (!hours >= 10)
        {
            time += "0";
        }
        time += hours + ":";

        if (!minutes >= 10)
        {
            time += "0";
        }
        time += minutes + ":";

        if (!seconds >= 10)
        {
            time += "0";
        }
        time += seconds;

        // Set the text to the textview
        counter.setText(time);
    }

    private class TimerUpdate extends TimerTask
    {
        @Override
        public void run()
        {
            updateClock();  
        }
    }

    @Override
    public View makeView() 
    {
        Log.d("MakeView");
        TextView t = new TextView(this);
        t.setTextSize(40);
        return t;
    }
}

So basically, I have a Timer that every second adds another second and them formats the way I want to be displayed and set the text of the TextSwitcher, which I thought called makeView, but makeView only gets called once and the time stays as 00:00:01. Did I miss a step, I dont think this UI object is very well documented.

Thanks, Jake

1

1 Answers

1
votes

You can only update the UI in the UI thread. So in your example you could do something like this.

private Handler mHandler = new Handler() {
     void handleMessage(Message msg) {
          switch(msg.what) {
               CASE UPDATE_TIME:
                    // set text to whatever, value can be put in the Message
          }
     }
}

And call

mHandler.sendMessage(msg);

in the run() method of the TimerTask.

This is a solution to your current issue but there is probably a better way to do it without using TimerTasks.