views:

43

answers:

1

I have a view and I want to add a chronometer onto it. Then, on draw I want to display the current time. Here is how I did:

    public class MyView extends View {
    private Chronometer chrono;
    private long elapsedTime=0;
    private String currentTime="00:00:00";

    public MyView(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    init(context);
    }

    private void init(Context ctx){
    chrono= new Chronometer(ctx);
    chrono.setText("Time: 00:00:00");
    chrono.setOnChronometerTickListener(new OnChronometerTickListener()
    {
           public void onChronometerTick(Chronometer arg0) {

             String HH =((elapsedTime / 3600) < 10 ? "0" : "") + (elapsedTime / 3600);
             String MM =((elapsedTime / 60) < 10 ? "0" : "") + (elapsedTime / 60); 
             String SS =((elapsedTime % 60) < 10 ? "0" : "") + (elapsedTime % 60);
             String currentTime = HH+":"+MM+":"+SS; 
             elapsedTime = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
             arg0.setText(currentTime);
           }
    }
    );
    }

    chrono.setBase(SystemClock.elapsedRealtime());
    chrono.start();

}
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawText(currentTime, someWidth , someHeight, somePaint);
    }

The initial text 00:00:00 gets drawn but it doesn't change. I must be doing something wrong. Any ideas ?

+1  A: 

You've made a local variable String currentTime and you are writing to that instead of your global variable.

Dazzy_G