How to get button background color on Android?

I want to get the color of the button. I could not get the color from the getbackground function, which returns drawable. I used getolidcolor, which returns an integer value, but its value is 0 (zero) all the time .. I do not understand where the problem is. perhaps this is not a real feature.

here is my android code

int renk = btn1.getSolidColor(); if(renk== Color.GREEN) Toast.makeText(getApplicationContext(), "green" , 1000).show(); else if(renk== Color.RED) Toast.makeText(getApplicationContext(), "red" , 1000).show(); else if(renk== Color.YELLOW) Toast.makeText(getApplicationContext(), "yellow" , 1000).show(); else Toast.makeText(getApplicationContext(), "unknown", 1000).show(); btn1.setBackgroundColor(Color.YELLOW); renk = btn1.getSolidColor(); if(renk== Color.GREEN) Toast.makeText(getApplicationContext(), "green" , 1000).show(); else if(renk== Color.RED) Toast.makeText(getApplicationContext(), "red" , 1000).show(); else if(renk== Color.YELLOW) Toast.makeText(getApplicationContext(), "yellow" , 1000).show(); else Toast.makeText(getApplicationContext(), "unknown", 1000).show(); 

I just get an unknown toast message, even if I set the background as yellow.

+7
source share
1 answer

Here I go ....

  Button myButton = (Button) findViewById(R.id.takePicture); myButton.setBackgroundDrawable(new PaintDrawable(Color.YELLOW)); PaintDrawable drawable = (PaintDrawable) myButton.getBackground(); int color = drawable.getPaint().getColor(); 
+8
source

All Articles