Automatically fade away after 3 seconds...

luisftv

Member
Licensed User
Longtime User
First, thank you in advance. I am new to Basic4Android and I love it. I am learning as I go. I’m new to programming too.

Here is the problem (task):

I made an app that generates a random number when pressing a button. Now, the number stays on the screen until I press again the button.

How do I make the number fade away (disappear) automatically after, say, three seconds. This way the screen goes blank and is ready for another random number. I am guessing the next number (as a game) but I don’t want to look at the already shown number. Get the idea?

Again, thank you.
 

mangojack

Well-Known Member
Licensed User
Longtime User
luisftv .. Welcome to the forum ...

This should get you going ...

B4X:
Sub Process_Globals
   
   Dim Timer1 As Timer
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   
   Timer1.Initialize("Timer1",3000)   'initialize timer , event = Timer1_Tick  , Interval = 3000 milliseconds.. = 3 secs
   'Timer1.Interval = 6000         'change the interval to 6 seconds
   
End Sub

Sub Button_Click
      
   Label1.Text = "7"               'or your random generated numbers
   Timer1.Enabled = True      'enable the timer .. which fires the Timer1_Tick event

End Sub

Sub Timer1_Tick
    
   'after the interval has elapsed .. ie 3 Secs Timer1 will blank Label1 and disable itself ...   
   Label1.Text = ""
   Timer1.Enabled=False
    
End Sub

You might want also want to read these posts as well
Timer Object
Using a Timer

Cheers mj
 
Last edited:
Upvote 0

IanMc

Well-Known Member
Licensed User
Longtime User
Excellent tutorial there by mangojack.

You see that the timer is set to an interval, when that interval is up the timers 'Tick' event is called.

This is a subroutine that is named with the name of your timer and then an underscore and then the word Tick

So after setting up the name of the timer and the interval that you want its Tick event to be called and here mangojack gives two times in milliseconds where 1000 milliseconds is 1 second so he shows how to set it to 3 seconds or to 6 seconds (the line to set it to six seconds is commented out) then all you have to do is to turn the timer on and off as you need it.

This is done by the line Timer1.Enabled =

so to turn the timer on you do Timer1.Enabled = True

mangojack shows this done in the Button_Click subroutine so that when the user clicks the button they also turn the timer on.

Then because Timer1 is turned on it will call its Tick subroutine after the set interval so in this case after three seconds Timer1_Tick is called and cleverly in the Timer1_Tick subroutine the timer is turned off again with:

Timer1.Enabled = False

because otherwise if you don't turn it off then that subroutine will be called continuously after each 3 second interval, which can also be a very powerful programming technique.

You'll begin to see a pattern of controls or views and their 'events' where you get the control or view name as you have called them so like
Dim ThisIsMyFirstTimer As Timer, then its event

Sub ThisIsMyFirstTimer_Tick

End Sub

and how to turn it on and off

ThisIsMyFirstTimer.Enabled =

and this pattern follows throughout B4a so it gets really easy to 'intuit' :)
 
Upvote 0

luisftv

Member
Licensed User
Longtime User
It works perfectly!

It works perfectly!

Thank you both MangoJack (for supplying the code and the links) and IanMc (for explaining it).

Basic4Android is so logical and straight forward, so easy... examples and explanations such as the one you both provided make it even easier, a snap. It took me longer to type the code verbatim that you provided than to understand it!

Nothing in life is difficult or complex... if the teacher explaining it has mastered the subject and applied it. :sign0188:

I can't thank you enough.
 
Upvote 0
Top