Skip to content Skip to sidebar Skip to footer

How To Get Background Color Of Button. On Click Action Depends On Background Color

Set background color to control(Button) from color.xml.... I need background color of button for check which color assigned that time.

Solution 1:

use like this:

It's easy to get this as a Drawable

Buttonbutton= (Button) findViewById(R.id.my_button);
DrawablebuttonBackground= button.getBackground();

If you know this is a color then you can try

ColorDrawablebuttonColor= (ColorDrawable) button.getBackground();

And if you're on Android 3.0+ you can get out the resource id of the color.

int colorId = buttonColor.getColor();

And compare this to your assigned colors, ie.

if (colorID == R.color.green) {
  log("color is green");
}

Post a Comment for "How To Get Background Color Of Button. On Click Action Depends On Background Color"