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: 890

rezaghasmi

Member
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


After execution, the code leaves the page


java.lang.IllegalStateException: Interval must be larger than 0.
at anywheresoftware.b4a.objects.Timer.setEnabled(Timer.java:79)
at b4a.example.rgeg._activity_create(rgeg.java:482)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at b4a.example.rgeg.afterFirstLayout(rgeg.java:104)
at b4a.example.rgeg.access$000(rgeg.java:17)
at b4a.example.rgeg$WaitForLayout.run(rgeg.java:82)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7561)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)
 
Upvote 0

John Naylor

Active Member
Licensed User
Longtime User
After execution, the code leaves the page


java.lang.IllegalStateException: Interval must be larger than 0.
at anywheresoftware.b4a.objects.Timer.setEnabled(Timer.java:79)
at b4a.example.rgeg._activity_create(rgeg.java:482)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:191)
at b4a.example.rgeg.afterFirstLayout(rgeg.java:104)
at b4a.example.rgeg.access$000(rgeg.java:17)
at b4a.example.rgeg$WaitForLayout.run(rgeg.java:82)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7561)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:995)

You haven't set your interval correctly here I would guess

B4X:
tmrCountdown.Initialize("tmr",1000)
 
Upvote 0

rezaghasmi

Member
timer.gif






B4X:
Sub Tmr_Tick
 If ( counter > 0) Then
      counter = counter - 1
     Dim sMin As Int = Floor(counter / 60)
     Dim sSec As Int = counter Mod 60
      Dim secPrefix As String =""
     Dim minPrefix As String =""
      If (sSec < 10) Then
      secPrefix = "0"
     End If
     If (sMin < 10 ) Then
       minPrefix = "0"
     End If
     txtCounter.Text = minPrefix & sMin &":"& secPrefix & sSec
     Else
     t.Enabled = False
 End If
 
End Sub
 
Upvote 0
Top