Android Question Timer

Olivier Zeegers

Member
Licensed User
I am trying to display a stopwatch to the user.

The moment the user pushes the record button and want to display in a label the time elapsed in format "hh:mm:ss:ms" and see the time running.

I assume one has to create a Timer Object and set it to enabled when the button is pressed and then in the Tick Sub update the label ? But how do I display it in this format ?

thank you
 

Jeffrey Cameron

Well-Known Member
Licensed User
When the user presses your start button, save Datetime.Now into a Long globals variable. In your Timer_Tick event, calculate the Elapsed by subtracting your start value from the current Datetime.Now value. Use this calculated long value to display in your label. something like:
B4X:
' Disclaimer:   TYPED IN FORUM EDITOR, NOT TESTED
Sub Timer_Tick()
    Label1.Text = FormatTime(Datetime.Now - glStart)
End Sub

Public Sub FormatTime(TickVal As Long) As String
    Dim plDiff As Long
    Dim psTemp As String
    Dim piTenth As Int
    Dim piSec As Int
    Dim piMin As Int
    Dim piHours As Int
    
    plDiff = TickVal
    
    If plDiff = 0 Then
        psTemp = "0:00:00"
    Else
        piTenth = plDiff / 100
        piSec = piTenth / 10
        piTenth = piTenth - (piSec * 10)
        piMin = piSec / 60
        piSec = piSec - (piMin * 60)
        piHours = piMin / 60
        piMin = piMin - (piHours * 60)
            
        
        psTemp = NumberFormat2(piHours, 1, 0, 0, False) & ":" & _
                 NumberFormat2(piMin, 2, 0, 0, False) & ":" & _
                 NumberFormat2(piSec, 2, 0, 0, False)
    End If

    Return psTemp
End Sub
 
Upvote 0

Olivier Zeegers

Member
Licensed User
When the user presses your start button, save Datetime.Now into a Long globals variable. In your Timer_Tick event, calculate the Elapsed by subtracting your start value from the current Datetime.Now value. Use this calculated long value to display in your label. something like:
B4X:
' Disclaimer:   TYPED IN FORUM EDITOR, NOT TESTED
Sub Timer_Tick()
    Label1.Text = FormatTime(Datetime.Now - glStart)
End Sub

Public Sub FormatTime(TickVal As Long) As String
    Dim plDiff As Long
    Dim psTemp As String
    Dim piTenth As Int
    Dim piSec As Int
    Dim piMin As Int
    Dim piHours As Int
   
    plDiff = TickVal
   
    If plDiff = 0 Then
        psTemp = "0:00:00"
    Else
        piTenth = plDiff / 100
        piSec = piTenth / 10
        piTenth = piTenth - (piSec * 10)
        piMin = piSec / 60
        piSec = piSec - (piMin * 60)
        piHours = piMin / 60
        piMin = piMin - (piHours * 60)
           
       
        psTemp = NumberFormat2(piHours, 1, 0, 0, False) & ":" & _
                 NumberFormat2(piMin, 2, 0, 0, False) & ":" & _
                 NumberFormat2(piSec, 2, 0, 0, False)
    End If

    Return psTemp
End Sub

thanx ! Does this Sub FormatTime that you wrote not exist in the B4A language ?
 
Upvote 0
Top