• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android Chronometer doesn't start

alinescoo

Lurker
Aug 20, 2010
5
7
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:

Code:
 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 ?
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones