Android Question Clock timer decrementer for OTP-Resend feature

beelze69

Active Member
Licensed User
Longtime User
Hi,

I would like to incorporate OTP-resend feature into my app.

For this, I need a clock timer (which will have a seconds count backwards)
from say 2 minute for the re-send OTP as follows:

The 'disabled' button named 'Re-send OTP' will display the clock ticking backwards 1 second and it will be 'enabled' once 2 minutes has lapsed.

Requesting for help on how to do this.

Thanks
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private btnOTP As Button
    Private timer As Timer
    Private timeout As Int
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
Sub OTP_Tick
    timeout = timeout-1
    btnOTP.Text = $"Timeout: ${timeout} sec."$
    If timeout <=0 Then
        btnOTP.Enabled = True
        btnOTP.Text = "Get OTP"
        timer.Enabled = False
    End If
End Sub
Private Sub btnOTP_Click
    timeout = 120
    btnOTP.Text = $"Timeout: ${timeout} sec."$
    btnOTP.Enabled = False
    timer.Initialize("OTP",1000)
    timer.Enabled = True
End Sub
 
Last edited:
Upvote 1

beelze69

Active Member
Licensed User
Longtime User
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private btnOTP As Button
    Private timer As Timer
    Private timeout As Int
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
Sub OTP_Tick
    timeout = timeout-1
    btnOTP.Text = $"Timeout: ${timeout} sec."$
    If timeout <=0 Then
        btnOTP.Enabled = True
        btnOTP.Text = "Get OTP"
        timer.Enabled = False
    End If
End Sub
Private Sub btnOTP_Click
    timeout = 120
    btnOTP.Text = $"Timeout: ${timeout} sec."$
    btnOTP.Enabled = False
    timer.Initialize("OTP",1000)
    timer.Enabled = True
End Sub
Hi,

Thanks a lot for the code...
 
Upvote 0
Top