package com.example.task03;

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

public class MainActivity extends Activity 
{
 
		private int cnt = 0;
 		Button Start, Stop;
 		TextView counter;
 		private Handler handler = new Handler();
 
 		/** Called when the activity is first created. */
 		@Override
 		public void onCreate(Bundle savedInstanceState) 
 		{
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_main);
	        counter = (TextView)findViewById(R.id.counter);
	        Start = (Button)findViewById(R.id.btn_start);
	        Start.setEnabled(true);
	        Stop = (Button)findViewById(R.id.btn_stop);
	        Stop.setEnabled(false);
	        
	        Start.setOnClickListener(new Button.OnClickListener()
	        {
				   @Override
				   public void onClick(View v) 
				   {
					    // TODO Auto-generated method stub
					    handler.post(timedTask);
					    Start.setEnabled(false);
					    Stop.setEnabled(true);
			   }
			});
        
	        Stop.setOnClickListener(new Button.OnClickListener()
	        {

				   @Override
				   public void onClick(View v)
				   {
				    // TODO Auto-generated method stub
					    handler.removeCallbacks(timedTask);
					    Start.setEnabled(true);
					    Stop.setEnabled(false);
					    cnt = 0;
					    counter.setText(String.valueOf(cnt));
				   }
			});
		}
    
 		private Runnable timedTask = new Runnable()
 		{

 			@Override
 			public void run() 
 			{
 				// TODO Auto-generated method stub
 				cnt++;
 				counter.setText(String.valueOf(cnt));
 				handler.postDelayed(timedTask, 2000);
 			}
 		};
}