That animation has initial acceleration and final deceleration, intentionally, if I'm not mistaken, so it should be tweaked a bit.[B4X] [XUI] [custom view] CircularProgressBar
This class is compatible with B4A, B4i and B4J. A simple circular progress bar implemented as a custom view. Usage: Add the class to your project and add a custom view with the designer. Set its value with" CircularProgressBar1.Value = 100 The animation is smooth in release mode...www.b4x.com
[B4X] [XUI] [custom view] CircularProgressBar
This class is compatible with B4A, B4i and B4J. A simple circular progress bar implemented as a custom view. Usage: Add the class to your project and add a custom view with the designer. Set its value with" CircularProgressBar1.Value = 100 The animation is smooth in release mode...www.b4x.com
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
I want the remaining value to be shown on the labelYou can use a regular timer with a duration of 1 second. Handle the timer_Tick event for reverse counting. If reached disable the timer.
I want the remaining value to be shown on the label
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
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
The code I posted is a bit better.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.
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
1. Add a reference to XUI library.How should I define please write؟
CountDown(YourLabel, 30)
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
1. Add a reference to XUI library.
2.B4X:CountDown(YourLabel, 30)
1. Add a reference to XUI library.
2.B4X:CountDown(YourLabel, 30)