Jimmi.Dvelo
Jimmi.Dvelo

Reputation: 51

timer android app start activity on create

I wrot a long time ago this code

public class MainActivity extends Activity {

 private Button startButton;     
    private Button stopButton;  
    private Button clearButton;
    private TextView timeValue;     
    private long timeStart = 0L;
    private Handler timeHandler = new Handler();
    long timeInMilisec = 0L;
    long timeMemo = 0L;
    long timeUpdate = 0L;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     timeValue = (TextView) findViewById(R.id.timeValue);            

        startButton = (Button) findViewById(R.id.startButton);      
        startButton.setOnClickListener(new View.OnClickListener() { 

            public void onClick(View view) {

                timeStart = SystemClock.uptimeMillis();     
                timeHandler.postDelayed(updateTimerThread, 0); 
            }
        });

        stopButton = (Button) findViewById(R.id.stopButton);        
        stopButton.setOnClickListener(new View.OnClickListener() {       

            public void onClick(View view) {        
                timeMemo += timeInMilisec;      
                timeHandler.removeCallbacks(updateTimerThread); 
            }
        });


     clearButton = (Button) findViewById(R.id.clearButton);
     clearButton.setOnClickListener(new View.OnClickListener() {                
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.clearButton:

                timeMemo = 0L;                  
                timeValue.setText("00:00:00");
                int secs = 0;       
                int mins = 0;       
                secs = 0;   
                int milliseconds = 0;
                timeInMilisec = SystemClock.uptimeMillis() - timeStart;     
                timeUpdate = timeMemo + timeInMilisec;      
                timeValue.setText("" + mins + ":"       
                        + String.format("%02d", secs) + ":"     
                        + String.format("%03d", milliseconds)); 

                timeHandler.removeCallbacksAndMessages(updateTimerThread);
                timeValue.setText("00:00:00");

                break;                  
            }                   
        }
    });

    }

     Runnable updateTimerThread = new Runnable() {       

        public void run() {

            timeInMilisec = SystemClock.uptimeMillis() - timeStart;     
            timeUpdate = timeMemo + timeInMilisec;      
            int secs = (int) (timeUpdate / 1000);       
            int mins = secs / 60;       
            secs = secs % 60;       
            int milliseconds = (int) (timeUpdate % 1000);       
            timeValue.setText("" + mins + ":"       
                    + String.format("%02d", secs) + ":"     
                    + String.format("%03d", milliseconds));     
            timeHandler.postDelayed(this, 0);              

        }

    };      

now I want my timer to start running when activity starts without pressing the start button (i want to delete it) I tried to put the code of the start button on the end of public void run() but it didnt work for me. how can i start the timer when activity launches?

Upvotes: 0

Views: 63

Answers (2)

NoChinDeluxe
NoChinDeluxe

Reputation: 3444

Just put the code from your startButton onClick directly into your onCreate() method. Like this...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //////////////////////////////////////////////////////////
    ///Put your timer start code here ///////////////////
    /////////////////////////////////////////////////////////
    timeStart = SystemClock.uptimeMillis();     
    timeHandler.postDelayed(updateTimerThread, 0); 
    //////////////////////////////////////////////////////////

    timeValue = (TextView) findViewById(R.id.timeValue);     

    stopButton = (Button) findViewById(R.id.stopButton);        
    stopButton.setOnClickListener(new View.OnClickListener() {       

        public void onClick(View view) {        
            timeMemo += timeInMilisec;      
            timeHandler.removeCallbacks(updateTimerThread); 
        }
    });


     clearButton = (Button) findViewById(R.id.clearButton);
     clearButton.setOnClickListener(new View.OnClickListener() {                
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.clearButton:

                timeMemo = 0L;                  
                timeValue.setText("00:00:00");
                int secs = 0;       
                int mins = 0;       
                secs = 0;   
                int milliseconds = 0;
                timeInMilisec = SystemClock.uptimeMillis() - timeStart;     
                timeUpdate = timeMemo + timeInMilisec;      
                timeValue.setText("" + mins + ":"       
                        + String.format("%02d", secs) + ":"     
                        + String.format("%03d", milliseconds)); 

                timeHandler.removeCallbacksAndMessages(updateTimerThread);
                timeValue.setText("00:00:00");

                break;                  
            }                   
        }
    });

    }

Upvotes: 0

U can simulate the button press calling onPerformClick() on method onResume()

@Override
public void onResume(){
  startButton.onPerformClick();
}

Upvotes: 1

Related Questions