Android Tutorial Prevent accidental clicks

I had issues when i did an animation like clicking on a button and shrink it that if you click twice on it it will perform 2 times or more the animation you want to do or if you start a task that can take some milliseconds like scrolling a panel from outside the screen to a specific position you really want to prevent the user to click on anything before you finished your task. this could lead to bugs in your app.

so until now what I did is I used a Boolean to check if the animation or that task has finished and then allowed the click event something like:

(click on the button to scroll the panel to be visible)

so first I set a boolean in globals
B4X:
dim animStart as Boolean

then I set it to TRUE before the animation. Now on each relevant button I check that boolean status like

B4X:
if NOT(animStart) then ...

and after the animation has finished I set it back to FALSE.

Now that's really a PITA and you will find yourself adding lots of checks to avoid accidental clicks.

so a MUCH simpler way to solve that issue is adding a panel for x ms and remove it after that to catch all your accidental clicks. it is much simpler to call that function and can be used in all b4x ide's

in b4a it could look like this:

B4X:
Sub freeze(Time As Int)
    Dim pnl As Panel
    pnl.Initialize("freezepnl")
    pnl.Color = Colors.Transparent
    pnl.BringToFront
    pnl.Elevation = 10dip
    Activity.AddView(pnl,0,0,100%x,100%y)
    Sleep(Time)
    pnl.RemoveView
End Sub

Sub freezepnl_Touch (Action As Int, X As Float, Y As Float)
    'do nothing
End Sub

and you can call it like this:

B4X:
Sub btn_Click
    myPanel.Left = 120%x
    myPanel.Visible = True
    myPanel.BringToFront
    myPanel.SetLayoutAnimated(500,0,0,myPanel.Width,myPanel.Height)
    freeze(500) '<----- we dont let any clicks on our activity for 500 ms the same time we use for our animation
End Sub

you could use #if B4A ... #if B4I... and make a code module for all ide's

if you have a better idea of how to prevent users to click anything while an animation is running for a specific time post your idea (code) here.

regards, ilan
 

sorex

Expert
Licensed User
Longtime User
I guess it all depends on how you structured everything.

I use the same approach in my games for the ingame menu and just use a gameOn boolean.

If the game starts I set gameOn=true and if I show a menu or dialog (game over, game completed...) I set gameOn=false.

the game and ingame menu use 1 seperate click event sub where the selection of 'command' is made by a select case based on the sender's tag.

this way you only need one if gameon=false then return at the game click event sub to ignore game clicks or not.

In some other cases I use your overlaying panel trick aswell for example if I want to darken the game field for better contrast of the hovering menu and it captures the clicks around the mnu aswell.
(actually it's just the menu panel that holds the buttons and other menu elements, no extra panel needed)

for animations I use my tween class that can work with call backs or skip new tweens if there's already tweens running.
 
Last edited:
Top