Android Question Count Up Timer

hello, can you help me with the code for a count up timer that when a button is clicked it will start counting up from 00:00:00 HH:mm:ss and will only stop if the user click the button again. Thanks
 
Solution
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Timer1 As Timer
    Private StartTime As Long
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Timer1.Initialize("Timer1", 500)
End Sub

Private Sub Timer1_Tick
    Dim milliseconds As Long = DateTime.Now - StartTime
    B4XPages.SetTitle(Me, ConvertMillisecondsToString(milliseconds))
End Sub

Private Sub Button1_Click
    If Timer1.Enabled = False Then
        StartTime = DateTime.Now
        Timer1.Enabled = True
    Else
        Timer1.Enabled = False
    End If
End Sub...

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Timer1 As Timer
    Private StartTime As Long
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Timer1.Initialize("Timer1", 500)
End Sub

Private Sub Timer1_Tick
    Dim milliseconds As Long = DateTime.Now - StartTime
    B4XPages.SetTitle(Me, ConvertMillisecondsToString(milliseconds))
End Sub

Private Sub Button1_Click
    If Timer1.Enabled = False Then
        StartTime = DateTime.Now
        Timer1.Enabled = True
    Else
        Timer1.Enabled = False
    End If
End Sub

'https://www.b4x.com/android/forum/threads/b4x-convert-milliseconds-to-string.89851/#content
Private Sub ConvertMillisecondsToString(t As Long) As String
    Dim hours, minutes, seconds As Int
    hours = t / DateTime.TicksPerHour
    minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
    seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
    Return $"$1.0{hours}:$2.0{minutes}:$2.0{seconds}"$
End Sub
 
Upvote 2
Solution

DonManfred

Expert
Licensed User
Longtime User
How to keep the count up timer running even I exit the application?
No timer runs when you exit your application. They are stopped when exiting.
 
Upvote 0
I am creating an app that monitors the time in and out of employees and once they timed in, the date and time will be saved on a database can i base the timer from the saved time so that upon exiting the application and run it again, it still counts the time elapsed since they have timed in?
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
You do not need to use the timer value at all. You have recorded the start time value. In the timer tick event handler you simply subtract the start time from DateTime.Now - that is the value that you display.
 
Upvote 1

joko0124

Member
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private Timer1 As Timer
    Private StartTime As Long
End Sub

Public Sub Initialize
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    Timer1.Initialize("Timer1", 500)
End Sub

Private Sub Timer1_Tick
    Dim milliseconds As Long = DateTime.Now - StartTime
    B4XPages.SetTitle(Me, ConvertMillisecondsToString(milliseconds))
End Sub

Private Sub Button1_Click
    If Timer1.Enabled = False Then
        StartTime = DateTime.Now
        Timer1.Enabled = True
    Else
        Timer1.Enabled = False
    End If
End Sub

'https://www.b4x.com/android/forum/threads/b4x-convert-milliseconds-to-string.89851/#content
Private Sub ConvertMillisecondsToString(t As Long) As String
    Dim hours, minutes, seconds As Int
    hours = t / DateTime.TicksPerHour
    minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
    seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
    Return $"$1.0{hours}:$2.0{minutes}:$2.0{seconds}"$
End Sub
Hi Sir!
How about adding milliseconds?
 
Upvote 0
Top