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