Android Question Change button colors

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Hi
I'm using a simple button. When pressed on the click action I'd like to change its color for a second, set it back to the original and only then execute the rest of click actions.
This is to give look & feel of the click.
Is there is simple way to do that?
I manage to change the color but it doesn't wait so the user can see the color change and back...

Anyone?

Thanks
 

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
I made a small test project and this code works:
B4X:
Private Sub btn_DoTransaction_Click

    Log("Wait")
    btn_DoTransaction.Background = cdClick
    btn_DoTransaction.TextColor = tColorClick

    Sleep(1000)
   
    btn_DoTransaction.Background = cdRelease
    btn_DoTransaction.TextColor = tColorRelease
   
    Log("Done")
'    HideAllPages
'    ShowTransactionPage

End Sub

Be aware that if the user clicks on the button before the second is passed the code will be executed once more.
The logs are just there to see that the program holds for 1 second.
Thanks, so you did it in the click sub not in another one. I did it with dedicated sub so i can use it for other buttons click, this way i need to do it for every button
I will try it thanks
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Hi
I'm using a simple button. When pressed on the click action I'd like to change its color for a second, set it back to the original and only then execute the rest of click actions.
This is to give look & feel of the click.
Is there is simple way to do that?
I manage to change the color but it doesn't wait so the user can see the color change and back...

Anyone?

Thanks
Btw, in windows i can set the form not active
Is there a way to set it not active in b4a?
I use panels so either to set the panel or the activity not active will prevent double click while color changing routine runs
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You could use this:
B4X:
Private Sub btn_DoTransaction_Click

    Log("Wait")
    btn_DoTransaction.Background = cdClick
    btn_DoTransaction.TextColor = tColorClick
    
    btn_DoTransaction.Enabled = False
    Sleep(1000)
    btn_DoTransaction.Enabled = True
    
    btn_DoTransaction.Background = cdRelease
    btn_DoTransaction.TextColor = tColorRelease
    
    Log("Done")
'    HideAllPages
'    ShowTransactionPage

End Sub
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Thanks, so you did it in the click sub not in another one. I did it with dedicated sub so i can use it for other buttons click, this way i need to do it for every button
This works too:
B4X:
Private Sub btn_DoTransaction_Click

    Log("Wait")
    
    ChangeColor(btn_DoTransaction)
    
    Log("Done")
    
'    HideAllPages
'    ShowTransactionPage

End Sub

Private Sub ChangeColor(btn As Button)
    btn.Background = cdClick
    btn.TextColor = tColorClick
    
    btn.Enabled = False
    Sleep(1000)
    btn.Enabled = True
    
    btn.Background = cdRelease
    btn.TextColor = tColorRelease
End Sub

Maybe, in your code the Try / Catch was a problem.
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
You could use this:
B4X:
Private Sub btn_DoTransaction_Click

    Log("Wait")
    btn_DoTransaction.Background = cdClick
    btn_DoTransaction.TextColor = tColorClick
  
    btn_DoTransaction.Enabled = False
    Sleep(1000)
    btn_DoTransaction.Enabled = True
  
    btn_DoTransaction.Background = cdRelease
    btn_DoTransaction.TextColor = tColorRelease
  
    Log("Done")
'    HideAllPages
'    ShowTransactionPage

End Sub

This works too:
B4X:
Private Sub btn_DoTransaction_Click

    Log("Wait")
   
    ChangeColor(btn_DoTransaction)
   
    Log("Done")
   
'    HideAllPages
'    ShowTransactionPage

End Sub

Private Sub ChangeColor(btn As Button)
    btn.Background = cdClick
    btn.TextColor = tColorClick
   
    btn.Enabled = False
    Sleep(1000)
    btn.Enabled = True
   
    btn.Background = cdRelease
    btn.TextColor = tColorRelease
End Sub

Maybe, in your code the Try / Catch was a problem.
Thanks, i know how to disable a button, can i disable the entire panel? Or activity?
 
Upvote 0

Zeev Goldstein

Well-Known Member
Licensed User
Longtime User
Folks - PROBLEM SOLVED!
i just used resumable sub on the sub that changes the color and it is all works like charm
thank you for your good will and ideas
 
Upvote 0
Solution
Top