Android Question Countdown timer

yiankos1

Well-Known Member
Licensed User
Longtime User
Hello my friends,
I searched a lot at forum but i couldn't figured out how to do it. I just need a countdown timer, that user will set how many hours/minutes/seconds wants to be and in every ticking will show how much time left (a normal countdown timer).
Thank you for your time.

p.s. Countdown timer library does not help me, cause you can't change anything, even font size.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You don't need a library for this.
Complete example:
B4X:
Sub Process_Globals
   Private timer1 As Timer
   Private targetTime As Long
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     timer1.Initialize("timer1", 1000)
   End If
   StartTimer(2, 30)
End Sub

Sub StartTimer (Hours As Int, Minutes As Int)
   targetTime = DateTime.Now + Hours * DateTime.TicksPerHour + Minutes * DateTime.TicksPerMinute
   timer1.Enabled = True
End Sub

Sub Timer1_Tick
   Dim t As Long = Max(0, targetTime - DateTime.Now)
   Dim  hours, minutes, seconds As Int
   hours = t / DateTime.TicksPerHour
   minutes = (t Mod DateTime.TicksPerHour) / DateTime.TicksPerMinute
   seconds = (t Mod DateTime.TicksPerMinute) / DateTime.TicksPerSecond
   Log($"$2.0{hours}:$2.0{minutes}:$2.0{seconds}"$)
   If t = 0 Then
     timer1.Enabled = False
   End If
End Sub
 
Upvote 0

yiankos1

Well-Known Member
Licensed User
Longtime User
Thank you very much for your answer, this is a perfectly working sample code. I have one more question that I couldn't figured out how to achieve it, is there any way this countdown timer to get paused and start again where it's paused?
I tried a few things but I couldn't achieved it, I think that the problem caused by DateTime.Now value because changes second by second and and i can't resume it where it paused.
Thank you for your time.
 
Upvote 0
Top