1. Check out our companion app, Forums for Android! Download from Google Play

Android Chronometer doesn't start

Discussion in 'Application Development' started by alinescoo, Oct 4, 2010.

  1. alinescoo

    alinescoo Member
    Thread Starter
    6

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

    Jan 30, 2011
    7
    0
    5
    did you manage to solve this? I'm in a similar situation.
     

Share This Page

Loading...