Search code examples
androidhandlerrunnableontouchlistenerandroid-timer

How can I check the duration after I click the button


I'm new to Java and Android, I would like to set up a time counter in my ACTION_UP event and cancel the timer while I do the other events. How can I basically set up a timer for that and stop and reset the timer for other event?


Solution

  • Timer start time.

    Set this in start timer click event.

    Date startDate = new Date();
    long startTime = 0;
    startTime = startDate.getTime();
    

    Store startTime in Global Variable so you can use that variable later.

    Timer end time.

    Set this in stop timer click event.

    Date endDate = new Date();
    long endTime = 0;
    endTime = endDate.getTime();
    

    Now get time difference in millisecond.

    long timeDiff = endTime - startTime;