1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.view;
18 
19 // Need the following import to get access to the app resources, since this
20 // class is in a sub-package.
21 import com.example.android.apis.R;
22 
23 import android.app.Activity;
24 import android.os.Bundle;
25 import android.os.SystemClock;
26 import android.view.View;
27 import android.view.View.OnClickListener;
28 import android.widget.Button;
29 import android.widget.Chronometer;
30 
31 public class ChronometerDemo extends Activity {
32     Chronometer mChronometer;
33 
34     @Override
onCreate(Bundle savedInstanceState)35     protected void onCreate(Bundle savedInstanceState) {
36         super.onCreate(savedInstanceState);
37 
38         setContentView(R.layout.chronometer);
39 
40         Button button;
41 
42         mChronometer = (Chronometer) findViewById(R.id.chronometer);
43 
44         // Watch for button clicks.
45         button = (Button) findViewById(R.id.start);
46         button.setOnClickListener(mStartListener);
47 
48         button = (Button) findViewById(R.id.stop);
49         button.setOnClickListener(mStopListener);
50 
51         button = (Button) findViewById(R.id.reset);
52         button.setOnClickListener(mResetListener);
53 
54         button = (Button) findViewById(R.id.set_format);
55         button.setOnClickListener(mSetFormatListener);
56 
57         button = (Button) findViewById(R.id.clear_format);
58         button.setOnClickListener(mClearFormatListener);
59     }
60 
61     View.OnClickListener mStartListener = new OnClickListener() {
62         public void onClick(View v) {
63             mChronometer.start();
64         }
65     };
66 
67     View.OnClickListener mStopListener = new OnClickListener() {
68         public void onClick(View v) {
69             mChronometer.stop();
70         }
71     };
72 
73     View.OnClickListener mResetListener = new OnClickListener() {
74         public void onClick(View v) {
75             mChronometer.setBase(SystemClock.elapsedRealtime());
76         }
77     };
78 
79     View.OnClickListener mSetFormatListener = new OnClickListener() {
80         public void onClick(View v) {
81             mChronometer.setFormat("Formatted time (%s)");
82         }
83     };
84 
85     View.OnClickListener mClearFormatListener = new OnClickListener() {
86         public void onClick(View v) {
87             mChronometer.setFormat(null);
88         }
89     };
90 }
91