Android Chronometer doesn't start


Last Updated:

  1. alinescoo

    alinescoo Member This Topic's Starter

    Joined:
    Aug 20, 2010
    Messages:
    5
    Likes Received:
    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 (Text):
    1.  public class MyView extends View {
    2.     private Chronometer chrono;
    3.     private long elapsedTime=0;
    4.     private String currentTime="00:00:00";
    5.  
    6.     public MyView(Context context) {
    7.     super(context);
    8.     // TODO Auto-generated constructor stub
    9.     init(context);
    10.     }
    11.  
    12.     private void init(Context ctx){
    13.     chrono= new Chronometer(ctx);
    14.     chrono.setText("Time: 00:00:00");
    15.     chrono.setOnChronometerTickListener(new OnChronometerTickListener()
    16.     {
    17.            public void onChronometerTick(Chronometer arg0) {
    18.  
    19.              String HH =((elapsedTime / 3600) < 10 ? "0" : "") + (elapsedTime / 3600);
    20.              String MM =((elapsedTime / 60) < 10 ? "0" : "") + (elapsedTime / 60);
    21.              String SS =((elapsedTime % 60) < 10 ? "0" : "") + (elapsedTime % 60);
    22.              String currentTime = HH+":"+MM+":"+SS;
    23.              elapsedTime = (SystemClock.elapsedRealtime() - arg0.getBase()) / 1000;
    24.              arg0.setText(currentTime);
    25.            }
    26.     }
    27.     );
    28.     }
    29.  
    30.     chrono.setBase(SystemClock.elapsedRealtime());
    31.     chrono.start();
    32.  
    33. }
    34.     @Override
    35.     protected void onDraw(Canvas canvas) {
    36.         canvas.drawText(currentTime, someWidth , someHeight, somePaint);
    37.     }
    The initial text 00:00:00 gets drawn but it doesn't change. I must be doing something wrong. Any ideas ?
     

    Advertisement
  2. danjpalmer

    danjpalmer Member

    Joined:
    Jan 30, 2011
    Messages:
    7
    Likes Received:
    0
    did you manage to solve this? I'm in a similar situation.
     

Share This Page

Loading...