Android Question Reverse timer

rezaghasmi

Member
How can I have a 30 second countdown timer?

img_20210210_232250_949-jpg.107787
 

Attachments

  • IMG_20210210_232250_949.jpg
    IMG_20210210_232250_949.jpg
    70.7 KB · Views: 928

MarcoRome

Expert
Licensed User
Longtime User
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
That animation has initial acceleration and final deceleration, intentionally, if I'm not mistaken, so it should be tweaked a bit.


Are you able to look at the circle?

1.gif
 
Upvote 0

rezaghasmi

Member


Thank
But dear friend, this code is too much and complicated

I just want to show the reverse of the activity from 30 to 0 in a tag after running
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Untested code:
B4X:
Sub CountDown (lbl As B4XView, Duration As Int)
    lbl.Text = Duration
    Dim target As Long = DateTime.Now + Duration * DateTime.TicksPerSecond
    Do While DateTime.Now < target
        lbl.Text = $"$1.0{(target - DateTime.Now) / DateTime.TicksPerSecond}"$
        Sleep(500)
    Loop
    lbl.Text = "0"
End Sub
 
Upvote 0

John Naylor

Active Member
Licensed User
Longtime User
I want the remaining value to be shown on the label

Quick knock up in B4J using a timer, the code remains the same in B4A for the countdown though....

Assumes a layout with a button and a label....

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Private Label1 As B4XView
  
    Dim tmrCountdown As Timer             'Timer control
    Dim tmrSeconds As Int                       'Countdown variable
  
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
  
    tmrCountdown.Initialize("tmr",1000)      'Initialise the timer to tick every 1 second
    tmrCountdown.Enabled=False               'Timers are initialised as enabled = false anyway but I like things to be very obvious
End Sub

Sub Button1_Click
  
    tmrSeconds=30    'Whatever you are counting down from
  
    tmrCountdown.Enabled = True      'Turn on the timer

End Sub

Sub tmr_tick
  
    tmrSeconds=tmrSeconds-1               'Reduce the countdown every time it ticks
    Label1.Text= tmrSeconds                  'Display current value on the label
  
    If tmrSeconds=0 Then tmrCountdown.Enabled=False      'Turn off the timer when we hit 0
  
End Sub
 
Last edited:
Upvote 0

rezaghasmi

Member
Quick knock up in B4J using a timer, the code remains the same in B4A for the countdown though....

Assumes a layout with a button and a label....

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Private Label1 As B4XView
 
    Dim tmrCountdown As Timer             'Timer control
    Dim tmrSeconds As Int                       'Countdown variable
 
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
 
    tmrCountdown.Initialize("tmr",1000)      'Initialise the timer to tick every 1 second
    tmrCountdown.Enabled=False               'Timers are initialised as enabled = false anyway but I like things to be very obvious
End Sub

Sub Button1_Click
 
    tmrSeconds=30    'Whatever you are counting down from
 
    tmrCountdown.Enabled = True      'Turn on the timer

End Sub

Sub tmr_tick
 
    tmrSeconds=tmrSeconds-1               'Reduce the countdown every time it ticks
    Label1.Text= tmrSeconds                  'Display current value on the label
 
    If tmrSeconds=0 Then tmrCountdown.Enabled=False      'Turn off the timer when we hit 0
 
End Sub

Thanks Let me go and test
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Declare a variable COUNT = 10

In your Timer tick event, decrease count by setting
Count = count - 1
The code I posted is a bit better.
1. It is very simple.
2. It is more accurate. Timers will never be 100% accurate as they run on the main thread. If the program is busy doing other stuff the counter will lag. With my code it will always stay accurate.
 
Upvote 0

rezaghasmi

Member
The code I posted is a bit better.
1. It is very simple.
2. It is more accurate. Timers will never be 100% accurate as they run on the main thread. If the program is busy doing other stuff the counter will lag. With my code it will always stay accurate.


How should I define please write؟🙏😓
 
Upvote 0

rezaghasmi

Member
Quick knock up in B4J using a timer, the code remains the same in B4A for the countdown though....

Assumes a layout with a button and a label....

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Private Label1 As B4XView
 
    Dim tmrCountdown As Timer             'Timer control
    Dim tmrSeconds As Int                       'Countdown variable
 
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
 
    tmrCountdown.Initialize("tmr",1000)      'Initialise the timer to tick every 1 second
    tmrCountdown.Enabled=False               'Timers are initialised as enabled = false anyway but I like things to be very obvious
End Sub

Sub Button1_Click
 
    tmrSeconds=30    'Whatever you are counting down from
 
    tmrCountdown.Enabled = True      'Turn on the timer

End Sub

Sub tmr_tick
 
    tmrSeconds=tmrSeconds-1               'Reduce the countdown every time it ticks
    Label1.Text= tmrSeconds                  'Display current value on the label
 
    If tmrSeconds=0 Then tmrCountdown.Enabled=False      'Turn off the timer when we hit 0
 
End Sub
IMG_20210211_123258_390.jpg


! ☹
 
Upvote 0

John Naylor

Active Member
Licensed User
Longtime User
Redone in B4A - Take into account Erel's comments about timers though

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
    
    Dim tmrCountdown As Timer
    Dim tmrSeconds As Int

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    Private Button1 As B4XView
    Private Label1 As B4XView
    
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    tmrCountdown.Initialize("tmr",1000)
    tmrCountdown.Enabled=False
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    
    tmrSeconds=30
    
    tmrCountdown.Enabled = True

End Sub

Sub tmr_tick
    
    tmrSeconds=tmrSeconds-1
    Label1.Text= tmrSeconds
    
    If tmrSeconds=0 Then tmrCountdown.Enabled=False
    
End Sub
 
Upvote 0
Top