javaandroidloopstimerchronometer

How can I make the Chronometer and Timer restart after 15 minutes?


I made a button that starts a Chronometer and I want to make the Chronometer restart after 15 minutes (to be looping). I am new to coding and I don't know how to manage that .

The code:

btPlay.setOnClickListener(v -> {

        chronometer.setBase(SystemClock.elapsedRealtime());
        chronometer.start();

    });

Solution

  • btPlay.setOnClickListener(v -> {
    
          startCounting=true;
    
        });
    
    myHandle = new Handler();
     myHandle.post(new Runnable() {
    
         @Override
         public void run() {
            myHandle.postDelayed(this, 1000);
            if(startCounting){
               //put your counting code here.
            }
         }
    
    });
    

    the void run function will get called every 1 second. you can add you code inside the run method.