-1

I can't seem to figure out how I can programmatically change the button text based on the current colour. For example I want to do something similar to the below pseudocode:

IF button colour == pink THEN
   set button text to "Pink"
ELSE 
   set button text to "Empty"
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
SumOne
  • 817
  • 3
  • 13
  • 24

1 Answers1

1

Assuming the background of the button is a color (not an image, for instance), then you can do it using the following code:

int color = ((ColorDrawable) button.getBackground()).getColor();
if(color == Color.rgb(pinkRed, pinkGreen, pinkBlue)) {
   button.setText("Pink");
} else {
   button.setText("Empty");
}
M A
  • 71,713
  • 13
  • 134
  • 174
  • Yes, its a colour. I will it a try and let you know :) – SumOne Jan 20 '17 at 20:16
  • I would also add that this logic sounds a bit weird. Why would the color change _itself_ have to change the button text? Maybe a better idea is to have whatever original event, which caused the color change, to also change the text as well. – M A Jan 20 '17 at 20:20
  • Good point :) apart from text change, I will also be handling other actions in this if statement. Can I check if its possbile to instead of using rgb to use hex values or normal colour names e.g. color.pink – SumOne Jan 20 '17 at 20:25
  • @SumOne Yes http://stackoverflow.com/questions/5248583/how-to-get-a-color-from-hexadecimal-color-string. There is no `Color.PINK`. – M A Jan 20 '17 at 20:30
  • great thanks, works – SumOne Jan 20 '17 at 20:37