Android Question B4A - How to use the timer - novice question

AlexOfOz

Active Member
Licensed User
Hello. I have looked through all documentation, code samples and videos that I can find, but I still can't figure out the sequence of coding for proper use of the timer.

I have the timer working for one off events, like delaying the changing of the colour of a button. I am very happy to achieve that. But now I'm trying to figure out how to - for example - make a series of buttons visible one at a time and, say, one second apart, or 2 or 3. The interval doesn't matter. But with the way I understand the timer to work, I can't figure out how to do this in the code.

Please help.
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Process_Globals
    Private tmr as Timer
End Sub

Sub Globals
    'Buttons that are declared in your layout & set to Visible = False
    Private btn1 as Button
    Private btn2 as Button
    Private btn3 as Button
    Private buttonIndex as Int = 0
    
    'Array to hold the buttons
    Private buttons() as Button = Array as Button(btn1, btn2, btn3)
End Sub

Sub Activity_Create(FirstTime as Boolean)
    Activity.LoadLayout("your_layout")

    tmr.Initialize("tmr", 2000)
    tmr.Enabbled = True
End Sub

Sub tmr_Tick
    buttons(buttonIndex).Visible = True
    buttonIndex = buttonIndex + 1
    If buttonIndex > buttons.Length - 1 Then tmr.Enabled = False
End Sub

Thrown together & totally untested, so might have some typo's / syntax errors - but should give you an idea...

- Colin.
 
Upvote 0

AlexOfOz

Active Member
Licensed User
Thanks Colin, I appreciate your help.

No the code doesn't work, but you have shown me aspects of the timer that I didn't know. I will play with this sample code, with my new understanding of how things are meant to fit together, and I'll eventually get it working.

Once I've got the timer thing under control, my next topic for learning will be accessing files in the cloud. I've already had a false start on that, but I'll come back to it after the timer is under control. This is a lot of fun.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Thanks Colin, I appreciate your help.

No the code doesn't work, but you have shown me aspects of the timer that I didn't know. I will play with this sample code, with my new understanding of how things are meant to fit together, and I'll eventually get it working.

Once I've got the timer thing under control, my next topic for learning will be accessing files in the cloud. I've already had a false start on that, but I'll come back to it after the timer is under control. This is a lot of fun.
OK - so if you use the code below it will work. There was 1 typo & 1 logic error in my previous example. You need to create a layout with the 3 buttons (btn1, btn2 & btn3) & call it "buttonlayout":
B4X:
Sub Process_Globals
    Private tmr As Timer
End Sub

Sub Globals
    'Buttons that are declared in your layout & set to Visible = False
    Private btn1 As Button
    Private btn2 As Button
    Private btn3 As Button
    Private buttonIndex As Int = 0
    
    'Array to hold the buttons
    Private buttons() As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("buttonlayout")
    buttons = Array As Button(btn1, btn2, btn3)
    tmr.Initialize("tmr", 2000)
    tmr.Enabled = True
End Sub

Sub tmr_Tick
    buttons(buttonIndex).Visible = True
    buttonIndex = buttonIndex + 1
    If buttonIndex > buttons.Length - 1 Then tmr.Enabled = False
End Sub

- Colin.
 
Upvote 0

AlexOfOz

Active Member
Licensed User
Sub Process_Globals
Private tmr As Timer
End Sub

Sub Globals
'Buttons that are declared in your layout & set to Visible = False
Private btn1 As Button
Private btn2 As Button
Private btn3 As Button
Private buttonIndex As Int = 0

'Array to hold the buttons
Private buttons() As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("buttonlayout")
buttons = Array As Button(btn1, btn2, btn3)
tmr.Initialize("tmr", 2000)
tmr.Enabled = True
End Sub

Sub tmr_Tick
buttons(buttonIndex).Visible = True
buttonIndex = buttonIndex + 1
If buttonIndex > buttons.Length - 1 Then tmr.Enabled = False
End Sub

Thanks Colin, it's working now.
 
Upvote 0
Top