Android Question Button pressed property

Peekay

Active Member
Licensed User
Longtime User
I have about twenty buttons numbered as 1 to 20 as their tags, on a panel.
I want the button that I press to be a pressed button and that its colour go to that of a pressed button, and I want to release all the other buttons, or at least the last one pressed, to be not pressed and return to its unpressed colour.
I can pick up the pressed button from the tag of the sender and I can remember that tag and the tag of the last button pressed.
When I press a button it goes to the pressed colour, but return to its unpressed colour as soon as I release the touch on that button.
How can I keep only one button pressed with its pressed colour.
I am used to VB6 where there are pressed buttons in an icon group and they become automatically unpressed when another icon is pressed.

Thanks
PK
 

klaus

Expert
Licensed User
Longtime User
I see three possibilities:
1. Use RadioButtons, your need is exactly what RadioButtons are made for.
2. Change the colors in the Click event.
You can memorize the clicked button to be able to reset to original color.
3. Use ToggleButtons, thei have two states ON and OFF.
You need to change to manage the ON / OFF states in the Click event.
 
Upvote 0

Peekay

Active Member
Licensed User
Longtime User
Klaus,

Thank you, I favour your solution and will try and implement.
Must I use tags or indexes to know which one was clicked and will the previous radiobutton be unclicked then?

PK
 
Last edited:
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Yes, but you could put more info into the tag: Array(index, state)

B4X:
Dim vx as B4XView = Sender
Dim info() as Object = vx.tag
Dim index as int = info(0)
Dim state As Boolean = info(1)
If state Then
     'turn it off
Else
     'turn the rest off
     'turn this on
End If
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Must I use tags or indexes to know which one was clicked and will the previous radiobutton be unclicked then?
Yes for the Tag, but the previous RadioButton will be unchecked automatically no need to do anything, this is the purpose of RadioButtons.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I agree about RadioButtons.

Note that if you set the color of a Button, it loses its original shape. If the project is B4A only, you could use StateListDrawable.

Without using Tags, it is useful to use a variable of type Button (if you do not use B4XView) declared at module level, which you will use to keep a reference to the last Button pressed, of which you will restore the original "color" (state) in the Click event.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…