I would like to know how to set the button color programatically? I have coded the following but fails:
Button11.setBackgroundColor(R.color.red);
Thanks!!
Answer
R.color.red
is an ID (which is also an int
), but is not a color.
Use one of the following instead:
// If you're in an activity: Button11.setBackgroundColor(getResources().getColor(R.color.red)); // OR, if you're not: Button11.setBackgroundColor(Button11.getContext().getResources().getColor(R.color.red));
Or, alternatively:
Button11.setBackgroundColor(Color.RED); // From android.graphics.Color
Or, for more pro skills:
Button11.setBackgroundColor(0xFFFF0000); // 0xAARRGGBB