Reading .Color attribute of Button

FrankR

Member
Licensed User
Longtime User
We can set the .Color attribute of a Button.
But we can't read it.
Can you consider adding that please? Thank you.

__________________________________________________________

Compiling code. Error
Error compiling program.
Error description: Property: Color is writeonly.
 

agraham

Expert
Licensed User
Longtime User
It's a bit more complicated than just getting a colour value. Setting the Color property actually sets the BackgroundDrawable of the Button to a ColorDrawable. Look in the help for Button.Color. You can get the actual background Drawable object using the Background property.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It's a bit more complicated than just getting a colour value. Setting the Color property actually sets the BackgroundDrawable of the Button to a ColorDrawable.
It's a bit more complicated. If the current drawable has round corners then instead of creating a ColorDrawable, the color value of the current drawable (which is of type GradientDrawable) is changed.
 

Shay

Well-Known Member
Licensed User
Longtime User
Can you explain this
if I set the button as button.color = colors.red
how can i read later the color of the button
 

Shay

Well-Known Member
Licensed User
Longtime User
this is not helping since I am using the tag to store different parameter

so another thing I thought about is to create another int for each button

for example

Button(1) will have also dim MyButton(1)
but how can I say:

if MyButton(---how do I get the button array number, which I get using "sender" --) = 1 then
Button.color = colors.red
end if
 

stevel05

Expert
Licensed User
Longtime User
I haven't tried it, but I'm sure I read on the forum that a tag can hold an object, so you could assign a list or array or even type to the tag and hold as many parameters as you wanted.

Steve
 

Shay

Well-Known Member
Licensed User
Longtime User
can you give example how i can do it?
store for example 2 numbers (can be from 1-200)
and then read each one of them
 

stevel05

Expert
Licensed User
Longtime User
Hi Shay,

Here's an example based on your first query, to help you properly we could really do with some more info. Like what are you trying to achieve, why do you want to know what colour the button is. If it's for a game and you are keeping tabs on numerous buttons, there are probably more efficient ways to do things.

Steve
 

Attachments

  • tagtest.zip
    5.2 KB · Views: 207

Shay

Well-Known Member
Licensed User
Longtime User
Thanks, but i am under NDA, so i am trying to ask without revealing the main project

so basically I if I can't read button color, how can i store 2 parameters in the tag for each button and read each parameter
so one will store number from 1-200 and the second will store my color (such as 1=red, 2=blue etc...)
 

kickaha

Well-Known Member
Licensed User
Longtime User
First you need to define a type (in Sub Process_Globals) :

B4X:
Type ButtonData (Value, bCol As Int)

Then you can set a buttons tag with:

B4X:
Dim myButton As ButtonData
myButton.Value = TopSecretValue
myButton.bCol = Colors.Red
Button.Color = Colors.Red
Button.Tag =  myButton
 

Shay

Well-Known Member
Licensed User
Longtime User
This is nice but still not helping (since I am using button array and I don't know how to read it)
is there a way to add more values to the Button
such as configure more parameters such as we have

Button.color
Button.tag
Can I add:
Button.My
Button.Another
 

kickaha

Well-Known Member
Licensed User
Longtime User
This is nice but still not helping (since I am using button array and I don't know how to read it)
is there a way to add more values to the Button
such as configure more parameters such as we have

Button.color
Button.tag
Can I add:
Button.My
Button.Another

My code above is how you effectively configure more parameters.

Even if what you put above was possible, you still would have the problem of not being able to read the button array.

post up some example code of your button array, and what you want to do with the buttons and we can come up with a solution.
 

Shay

Well-Known Member
Licensed User
Longtime User
let say I want to build the battleship game
I have 2 players (2 sides), each has 10 ships (2 x 10 buttons by array)
each ship button can have 3 modes: not hit, hit, sunk
when ship hit it has yellow color, when sunk has red color

when clicking on ship, I need to check the ship number, color, and status (none, hit, sunk) and if none, color will change to yellow, if was yellow so now it will be red
of course I need to choose the right 10 buttons (ships) based on the player

so I need to store lots of info for each button

I hope this is helping for what I am trying to do
 

klaus

Expert
Licensed User
Longtime User
In your example, I would manage the colors with the status parameter.
Let
status = 0 , none
status = 1 , hit
status = 2 , sunk

then have a color array
AreaColor(0) = Colors.Blue ,none
AreaColor(1) = Colors.Yellow , hit
AreaColor(2) = Colors.Red ,sunk

According to the button's staus value you set Button.Color = AreaColor(ButtonStatus)

ButtonStatus could be an array with the button indexes.

Best regards.
 

Shay

Well-Known Member
Licensed User
Longtime User
how do I know which player button were pressed (1 or 2)

how do I read the button color / status?

how do I join your example to button?

(can you give code example)

mayber another idea - store all in the button.tag
mybutton.tag = ship_number, color, etc...
how to extract the numbers:
1. how to split by ","
2. how to add one of the parametes to dim - meaninng:
MyShipNumber = split(D.tag,","1)
MyShipColor = split(D.tag,","2)

wow this is complicated with this software.....
 

kickaha

Well-Known Member
Licensed User
Longtime User
how do I know which player button were pressed (1 or 2)

how do I read the button color / status?

how do I join your example to button?

(can you give code example)

mayber another idea - store all in the button.tag
mybutton.tag = ship_number, color, etc...
how to extract the numbers:
1. how to split by ","
2. how to add one of the parametes to dim - meaninng:
MyShipNumber = split(D.tag,","1)
MyShipColor = split(D.tag,","2)

wow this is complicated with this software.....

To store the data in the tag (as I said earlier), you need to define a type (in Sub Process_Globals) with all the data you want to store:

B4X:
Type TagData (ShipNumber, ShipColor As Int, ShipStatus As String)

You can then add this to the button:
B4X:
Dim myTag As TagData
myTag.ShipNumber= 1
myTag.ShipColor = Colors.Red
myTag.ShipStatus = "Sunk"
button.Tag =  myTag

You can retrieve the values by:
B4X:
Dim myTag As TagData
myTag= button.Tag
MyShipNumber = myTag.ShipNumber
MyShipColor = myTag.ShipColor
ShipStatus = myTag.ShipStatus
That way you can store any number of any type of variables you want in the Tag, and can get at them using their names.

You do however need to know what button you want the data from!
 

Shay

Well-Known Member
Licensed User
Longtime User
I think I am still missing on how to know which button from the array was pressed
since I am using the d = sender
 

kickaha

Well-Known Member
Licensed User
Longtime User
I think I am still missing on how to know which button from the array was pressed
since I am using the d = sender

So, if you have a common button click sub, with my above code:
B4X:
Dim myTag As TagData
myTag= Sender.Tag
If you have set the type up with a value to identify the button:
B4X:
Type TagData (ShipNumber, ShipColor As Int, ShipStatus, ButtonIdent As String)
you can then retrieve it here.
B4X:
ButtonIdent = myTag.ButtonIdent
 
Top