SoFunction
Updated on 2025-04-04

Android programming timer Chronometer simple example

This article describes the Android timer Chronometer. Share it for your reference, as follows:

The Chronometer control in Android is inherited from TextView. This component can be timed at 1 second interval and displays the timing results. This is what we often call a timer tool.

public class ChronometerActivity extends Activity implements OnClickListener {
  private Chronometer mChronometer;
  private Button start, stop, reset, format, clear_format;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_chronometer);
    // Initialize the View widget    initViews();
    // Set up listening events    initListeners();
  }
  private void initListeners() {
    (this);
    (this);
    (this);
    (this);
    clear_format.setOnClickListener(this);
  }
  private void initViews() {
    mChronometer = (Chronometer) findViewById();
    start = (Button) findViewById();
    stop = (Button) findViewById();
    reset = (Button) findViewById();
    format = (Button) findViewById(.set_format);
    clear_format = (Button) findViewById(.clear_format);
  }
   mStartListener = new OnClickListener() {
    public void onClick(View v) {
      ();
    }
  };
   mStopListener = new OnClickListener() {
    public void onClick(View v) {
      ();
    }
  };
   mResetListener = new OnClickListener() {
    public void onClick(View v) {
      (());
    }
  };
   mSetFormatListener = new OnClickListener() {
    public void onClick(View v) {
      ("Formatted time (%s)");
    }
  };
   mClearFormatListener = new OnClickListener() {
    public void onClick(View v) {
      (null);
    }
  };
  @Override
  public void onClick(View v) {
    switch (()) {
    case :
      ();// Start timing      break;
    case :
      ();// Pause timekeeping      break;
    case :
      (());// milliseconds from booting up now      break;
    case .set_format:
      // Requires a String variable and uses "%s" to represent the timing information      ("Time cumulative: %s seconds");
      break;
    case .clear_format:
      (null);
      break;
    }
  }
}

Layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:gravity="center_horizontal"
  android:orientation="vertical"
  android:padding="10dip" >
  <Chronometer
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:format="@string/chronometer_initial_format"
    android:paddingBottom="30dip"
    android:paddingTop="30dip"
    android:textSize="15sp" />
  <Button
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="start" >
    <requestFocus />
  </Button>
  <Button
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="pause" >
  </Button>
  <Button
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Re-time" >
  </Button>
  <Button
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Information description" >
  </Button>
  <Button
    android:
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Cancel Instructions" >
  </Button>
</LinearLayout>

PS: Here are a few related online tools for your reference:

Online stopwatch tool:
http://tools./bianmin/miaobiao

Unix timestamp conversion tool:
http://tools./code/unixtime

For more information about Android related content, please check out the topic of this site:Android date and time operation skills summary》、《Android development introduction and advanced tutorial》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.