Search code examples
androidif-statementbuttononclickchronometer

How to make a method similar to on / off button using if-else


I try to put the control of the chronometer on the button, for start and stop. Tried to implement it, but the result is not the same, help with the advice?

 public void onStartClick(View view){
    mChronometer.setBase(SystemClock.elapsedRealtime());
    if(mButton.isPressed()==false){
        mChronometer.start();}else{
        mChronometer.stop();
    }
}

Solution

  • Something like this would be easiest is my opinion:

     boolean mIsOn = false;
     public void onStartClick(View view){
       mChronometer.setBase(SystemClock.elapsedRealtime());
       if(!mIsOn){
         mChronometer.start();
       }
       else{
         mChronometer.stop();
    
       }
       mIsOn = !mIsOn;
    

    }