3

How do you get the background color of a button? I've tried the following:

 public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn1 = findViewById(R.id.button);
        btn1.setBackgroundColor(getResources().getColor(R.color.red));
        //color red is added to colors.xml <color name="red">#FF0000</color>

        btn1.setOnClickListener(v -> {

          ColorDrawable btnColor = (ColorDrawable) btn1.getBackground();

          int clr = btnColor.getColor();

          if (clr == getResources().getColor(R.color.red)) {
              String line = "it's red";
              btn1.setText(line);
            }
        });
    }
}

When I click the button the app closes and I get this

 java.lang.ClassCastException: android.graphics.drawable.RippleDrawable cannot be cast to android.graphics.drawable.ColorDrawable

Can anyone explain what I'm doing wrong?

Anand
  • 857
  • 1
  • 12
  • 18
jason
  • 41
  • 4

3 Answers3

1

Since you are using a MaterialComponents theme your Button is replaced at runtime by a MaterialButton.

Use the setBackgroundTintList instead of the setBackgroundColor and use getBackgroundTintList() to retrieve the ColorStateList.

Something like:

    MaterialButton button = findViewById(R.id.button);
    button.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(this, R.color.red600)));
    ColorStateList colorStateList = button.getBackgroundTintList();

    int defaultColor = colorStateList.getColorForState(
            new int[] { android.R.attr.state_enabled},0);
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • the way I'm understanding this approach is that you are getting the color assigned to the given state. Which is technically different than finding the current color of the button. Is there a way to programmatically get at the current state of the control (rather than directly pass in the android.R.attr.state_enabled)? – evve Oct 26 '22 at 20:55
0

you can get this as a drawable:

Button mButton = (Button) findViewById(R.id.button);
Drawable buttonBackground = mButton.getBackground();

if its a color you can do like this:

ColorDrawable btnColor = (ColorDrawable) button.getBackground();

get out resource id of the color:

Int color = btnColor.getColor();

then compare this with your color:

if (color == R.color.green) {
   log("color is green");
}
Hassan
  • 380
  • 6
  • 20
  • Sorry I'm pretty new to Java and the android studio, but isn't what you posted what I've got except with the if statement, mine sets the text of a button instead of posting to 'log'? – jason May 24 '21 at 12:29
-1
ColorDrawable buttonColor = (ColorDrawable) button.getBackground();
int colorId = buttonColor.getColor();
if (colorID == R.color.green) {
  log("color is green");
}

Using the above you can find a button color.

Benkerroum Mohamed
  • 1,867
  • 3
  • 13
  • 19