[Android] Simple repeat timer count

I practiced to make timer base app.

When start button is pushed, it starts to count up and shows counted value in the label.
When stop button is pushed, it stops to count up.

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/currentTimeLabel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/startButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start" />

        <Button
            android:id="@+id/stopButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop" />

    </LinearLayout>

</LinearLayout>

TimerSampleActivity.java

package com.mytimer;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class TimerSampleActivity extends Activity implements View.OnClickListener{
    private final String TAG = "TimerSample";  
	
	private Button mStartButton;
	private Button mStopButton;
	public int currentTime;
	private Handler _myHandler;
	private TextView mCurrentTimeLabel;
	private Runnable _myTask;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        currentTime = 0;
        mStartButton = (Button)findViewById(R.id.startButton);
        mStopButton = (Button)findViewById(R.id.stopButton);
        mCurrentTimeLabel = (TextView)findViewById(R.id.currentTimeLabel);
        mStartButton.setOnClickListener(this);
        mStopButton.setOnClickListener(this);
    }

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.startButton){
			Log.d(TAG, "start");
			cancelTimer();
			_myTask = new MyTimerTask();
			_myHandler = new Handler();
			_myHandler.postDelayed(_myTask, 100);
		}
		else if(v.getId() == R.id.stopButton){
			Log.d(TAG, "stop");
			mCurrentTimeLabel.setText("stop");
			cancelTimer();
		}
		
	}
	
	private void cancelTimer()
	{
		if(_myHandler != null){
			_myHandler.removeCallbacks(_myTask);
		}
	}
	
	private class MyTimerTask implements Runnable
	{
		@Override
		public void run() {
			currentTime++;
			mCurrentTimeLabel.setText(Integer.toString(currentTime));
			
			_myHandler.postDelayed(this, 100);
		}
	}
}

Below page is original source code page.
Updating the UI from a Timer

Tags:

Comments are closed.