Monday, June 8, 2015

// //

Proper use and how to use android Chronometer

Android chronometer start
Android Chronometer Started

Chronometer stop:
android Chronometer stop





hi, this time i'll be dealing with on proper used of android chronometer. activity_main.xml

         
    
     







     
     
     
            
       
    
     
     

     

 

MainActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.ViewFlipper;

public class MainActivity extends Activity implements OnClickListener {
 Chronometer mRecordChronometer;
  private Button start, stop, restart;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mRecordChronometer = (Chronometer)findViewById(R.id.chronometer);
  
 start = (Button) findViewById(R.id.start);
         stop = (Button) findViewById(R.id.stop);
         restart = (Button) findViewById(R.id.restart);
          
         start.setOnClickListener(this);
         stop.setOnClickListener(this);
         restart.setOnClickListener(this);
  
 }
 @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
 
   switch(v.getId()) {
         case R.id.start : 
           mychromestart();
         break;
          
         case R.id.stop: 
          
           mychromestop();
         break;
          
         case R.id.restart: 
          mychromestart();
          
         break;
         }
  
 }

 private void mychromestart() {
  // TODO Auto-generated method stub
   mRecordChronometer.setBase(SystemClock.elapsedRealtime());
   mRecordChronometer.start();
 }

 private void mychromestop() {
  // TODO Auto-generated method stub
   mRecordChronometer.setBase(SystemClock.elapsedRealtime());
   mRecordChronometer.stop();
 }
 
 private void mychromerestart() {
  // TODO Auto-generated method stub
   mRecordChronometer.setBase(SystemClock.elapsedRealtime());
   mRecordChronometer.stop();
 }
}

Don't Hesitate to comment if you are having problems.